Generic Lists

Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents

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.

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

List.Enumerator structure

The List.Enumerator implements the IEnumerator<T> interface for a generic List.

See IEnumerator and IEnumerator<T>.

SortedList class

prevpp.png  nextpp.png
C# Online.NET