C # objects in arrays

I am working with ArrayListin C # and I wonder how can I add objects to ArrayListand then extract values ​​from it?

In short, how can I add, delete, edit, and read from ArrayListclasses containing objects?

Thanks for the help!

+5
source share
4 answers

If you are not in a situation where you must use .NET 1.0 / 1.1 or must interact with legacy code that uses ArrayList, you should really avoid using ArrayListsin new code. Use a generic collection type List<> .

, List<T> .

, Animal, :

Animal dog = new Animal("dog");
Animal cat = new Animal("cat");

List<Animal> animalList = new List<Animal>();

// example of adding items to the list
animalList.Add( dog );
animalList.Add( cat );

// example of removing items form the list
animalList.Remove( cat );

// example of replacing an item at a given position
animalList[0] = new Animal("giraffe");

List<T> ArrayList . , ArrayList object, , .NET .

ArrayList listOfObjects = new ArrayList();
int myAge = 207;
listOfObjects.Add( (object)myAge );

, int ( .NET) . , int ArrayList. ArrayList - List<T>, . , ArrayList . :

listOfObjects.Add( (object)myAge );
listOfObjects.Add( "Hello World" );

. ArrayList , . ArrayList , , , ArrayList, . List<T> , , (, T List<T>).

- . . , .NET/#, - . . . .. , , , . # , . , . , , , - : , , , ..

+23

-, List, ArrayList #. , :

List<String> myList = new List<String>();

var myList = new List<String>();

, .

myList.Add("bla");
var test = myList[0];
+1

, ( .add).

msdn

0

im, , im . , , ..:/im , java ArrayList. bcz mine, : (

= ();

person p2 = new person("Hammad", "Lahore", 1, 123);
person p3 = new person("adnan", "Lahore", 1, 123);
person p4 = new person("qamar", "Lahore", 1, 123);
lists.Add(p2);
0

All Articles