holidaypasob.blogg.se

Csharp array vs arraylist
Csharp array vs arraylist




csharp array vs arraylist csharp array vs arraylist

The Count property, on the other hand, identifies how many items are currently stored in the list. The Capacity property can be used to identify how many items a collection can store without having to resize. There are two class members that are useful for obtaining information about a C# List or ArrayList collection object. This is particularly useful when a list contains duplicate items. The LastIndexOf() method returns the index value of the last item in the list to match the specified item. For example, without knowing in advance the index value of the "Yellow" string we can change to "Black": colorList = "Black"

csharp array vs arraylist

This technique could be used to replace a specific value with another. If the item is not found in the List a value of -1 is returned by the IndexOf() method.

#CSHARP ARRAY VS ARRAYLIST CODE#

For example, the following code sample will output the value 2, which is the index position of the "Yellow" string: List colorList = new List() Ĭonsole.WriteLine(colorList.IndexOf("Yellow")) The IndexOf() method returns the index value of a matching item in a list. The most basic method is the Contains() method, which when called on a List or ArrayList object returns true if the specified item is found in the list, or false if it is not. If the items in a list are required to be always sorted into order the Sort() method should be called after new items are added: colorList.Sort() Ī number of methods are provided with the List and ArrayList classes for the purposes of finding items. There is no way to tell C# to automatically sort a list as items are added.






Csharp array vs arraylist