List in C# / PDF

List in C# / PDF









List in C# / PDF












What are C# Collection Classes

 The C# Collection classes are a set of classes designed specifically for grouping together objects and performing tasks on them. A number of collection classes are available with C#.

Creating C# List Collections - List and ArrayList 

Both the List and ArrayList classes have properties very similar to C# arrays. One key advantage of these classes over arrays is that they can grow and shrink as the number of stored objects changes. The List class is contained with the System.Collections.Generic namespace whilst the ArrayList class is contained within the System.Collections namespace. The syntax for creating a List collection is as follows: List name = new List(); An ArrayList object is created in a similar manner, although without the type argument: ArrayList name = new ArrayList(); With the above syntax in mind we can now create a List object called colorList:

using System; using System.Collections.Generic; public class Lists { static void Main() { List colorList = new List(); } }

Adding Items to Lists

 Once a List object has been created there are a number of methods which may be called to perform tasks on the list. Once such method is the Add() method which, as the name suggests, is used to add items to the list object:

List colorList = new List(); 
 colorList.Add ("Red");
 colorList.Add ("Green");
 colorList.Add ("Yellow"); colorList.Add ("Purple"); colorList.Add ("Orange");


Accessing List Items

Individual items in a list may be accessed using the index value of the item (keeping in mind that the first item is index 0, the second index 1 and so on). The index value is placed in............










Download List in C# / PDF







List in C# / PDF

0 commentaires: