Generic Lists
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
Contents |
[edit]
List class
List<T> is the most useful collection. It is basically an array that can change size dynamically.
Since it is a generic collection, it is also type safe.
[edit]
Code Examples
Add items to a list
List<int> numbers = new List<int>(); numbers.Add( 1 ); numbers.Add( 2 ); numbers.Add( 3 );
Does a list contain an item
if( numbers.Contains( 3 ){ //... }
Remove items from a list
numbers.Remove( 1 ); numbers.Remove( 2 ); numbers.Remove( 3 );
Remove the first 3 items in a List
// When an item is removed all, // items following it are moved to fill the gap // So in order to remove the first 3 items, // Remove from the first item 3 times numbers.RemoveAt( 0 ); numbers.RemoveAt( 0 ); numbers.RemoveAt( 0 );
[edit]
List.Enumerator structure
The List.Enumerator implements the IEnumerator<T> interface for a generic List.
See IEnumerator and IEnumerator<T>.
[edit]