Mega Code Archive

 
Categories / C# Book / 03 Collections
 

0353 List

The followoing code is the structure of List class. public class List <T> : IList <T> { public List (); public List (IEnumerable<T> collection); public List (int capacity); // Add+Insert public void Add (T item); public void AddRange (IEnumerable<T> collection); public void Insert (int index, T item); public void InsertRange (int index, IEnumerable<T> collection); // Remove public bool Remove (T item); public void RemoveAt (int index); public void RemoveRange (int index, int count); public int RemoveAll (Predicate<T> match); // Indexing public T this [int index] { get; set; } public List<T> GetRange (int index, int count); public Enumerator<T> GetEnumerator(); // Exporting, copying and converting: public T[] ToArray(); public void CopyTo (T[] array); public void CopyTo (T[] array, int arrayIndex); public void CopyTo (int index, T[] array, int arrayIndex, int count); public ReadOnlyCollection<T> AsReadOnly(); public List<TOutput> ConvertAll<TOutput> (Converter <T,TOutput> converter); // Other: public void Reverse(); public int Capacity { get;set; } public void TrimExcess(); public void Clear(); } The following code shows how to add elements a List. using System; using System.Collections; using System.Collections.Generic; class Sample { public static void Main() { //Create a List of Strings //String-typed list List<string> words = new List<string>(); words.Add("A"); words.Add("B"); words.AddRange(new[] { "C", "D" }); words.Insert(0, "A"); // Insert at start words.InsertRange(0, new[] { "E", "F" }); } } Remove elements from a List using System; using System.Collections; using System.Collections.Generic; class Sample { public static void Main() { //Create a List of Strings //String-typed list List<string> words = new List<string>(); words.Add("A"); words.Add("B"); words.Add("C"); words.Add("D"); words.Add("E"); words.Add("F"); words.Remove("A"); words.RemoveAt(3); // Remove the 4th element words.RemoveRange (0, 2); // Remove first 2 elements // Remove all strings starting in 'n': words.RemoveAll(s => s.StartsWith("n")); } } Get elements from List using System; using System.Collections; using System.Collections.Generic; class Sample { public static void Main() { //Create a List of Strings //String-typed list List<string> words = new List<string>(); words.Add("A"); words.Add("B"); words.Add("C"); words.Add("D"); words.Add("E"); words.Add("F"); Console.WriteLine("First:"+words[0]); // first word Console.WriteLine ("Last:"+words[words.Count - 1]); // last word foreach (string s in words) Console.WriteLine ("all:"+s); // all words IList<string> subset = words.GetRange (1, 2); // 2nd->3rd words } } The output: First:A Last:F all:A all:B all:C all:D all:E all:F Convert List to an Array using System; using System.Collections; using System.Collections.Generic; class Sample { public static void Main() { //Create a List of Strings //String-typed list List<string> words = new List<string>(); words.Add("A"); words.Add("B"); words.Add("C"); words.Add("D"); words.Add("E"); words.Add("F"); string[] wordsArray = words.ToArray(); // Creates a new typed array // Copy first two elements to the end of an existing array: string[] existing = new string[1000]; words.CopyTo(0, existing, 998, 2); } } Convert List element using System; using System.Collections; using System.Collections.Generic; class Sample { public static void Main() { //Create a List of Strings //String-typed list List<string> words = new List<string>(); words.Add("A"); words.Add("B"); words.Add("C"); words.Add("D"); words.Add("E"); words.Add("F"); List<string> upperCastWords = words.ConvertAll(s => s.ToUpper()); List<int> lengths = words.ConvertAll(s => s.Length); } }