0

I am completely new to c#, so please help me with the following problem.

I have an array list with some string elements.I want to edit/update a particular element at a particular index.

Can anyone please tell me a method, other than removing and then adding the edited element at the specific index?

Please find my try below[Changed the first element from "Black" to "Grey"]

ArrayList al = new ArrayList(){ "Black", "White", "Red"};

al.RemoveAt(0);

al.Insert(0,"Grey");
2
  • Why would you ever use ArrayList anyway? It's deprecated in favour of the much better List<T> Commented Oct 31, 2021 at 19:59
  • Just for learning purpose, as i told i am new to C#. Thank you. Commented Oct 31, 2021 at 20:17

2 Answers 2

2

Try this:

ArrayList al = new ArrayList(){ "Black", "White", "Red"};
al[0] = "Grey";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Never had a thought in this way,
1

The ArrayList type has an indexer you can use.

ArrayList al = new ArrayList { "Black", "White", "Red"};

al[0] = "Grey";

For more information on indexers see:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.