Home > Mobile >  How to edit/update arraylist values in c#
How to edit/update arraylist values in c#

Time:11-01

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");

CodePudding user response:

Try this:

ArrayList al = new ArrayList(){ "Black", "White", "Red"};
al[0] = "Grey";

CodePudding user response:

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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/

  • Related