Collection Interfaces | ||||||||||||||
The .NET Framework provides two sets of standard interfaces for enumerating and comparing collections: the traditional (nontype-safe) and the new generic type-safe collections. Here we focuses only on the new, type-safe collection interfaces as these are far preferable. | ||||||||||||||
You can declare an ICollection of any specific type by substituting the actual type (for example, int or string) for the generic type in the interface declaration ( ). | ||||||||||||||
TIP: C++ programmers note: C# generics are similar in syntax and usage to C++ templates. However, because the generic types are expanded to their specific type at runtime, the JIT compiler is able to share code among different instances, dramatically reducing the code bloat that you may see when using templates in C++. | ||||||||||||||
The key generic collection interfaces are listed in the given Table: | ||||||||||||||
Table: Collection interfaces | ||||||||||||||
|
||||||||||||||
The IEnumerable<T> Interface | ||||||||||||||
You can support the foreachstatement in ListBoxTest by implementing the IEnumerable<T> interface (see in the given Example-6). IEnumerable has only one method, GetEnumerator( ), whose job is to return an implementation of IEnumerator<T>. The C# language provides special help in creating the enumerator, using the new keyword yield. | ||||||||||||||
Example-6 Making a ListBox an IEnumerable class | ||||||||||||||
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace Enumerable { public class ListBoxTest : IEnumerable { private string[] strings; private int ctr = 0; // Enumerable classes can return an enumerator public IEnumerator GetEnumerator( ) { foreach ( string s in strings ) { yield return s; } } // initialize the list box with strings public ListBoxTest( params string[] initialStrings ) { // allocate space for the strings strings = new String[8]; // copy the strings passed in to the constructor foreach ( string s in initialStrings ) { strings[ctr++] = s; } } // add a single string to the end of the list box public void Add( string theString ) { strings[ctr] = theString; ctr++; } // allow array-like access public string this[int index] { get { if ( index < 0 || index >= strings.Length ) { // handle bad index } return strings[index]; } set { strings[index] = value; } } // publish how many strings you hold public int GetNumEntries( ) { return ctr; } } public class Tester { static void Main( ) { // create a new list box and initialize ListBoxTest lbt = new ListBoxTest( "Hello", "World" ); // add a few strings lbt.Add( "www.csitquestions.com" ); lbt.Add( "Pvt" ); lbt.Add( "Ltd" ); lbt.Add( "Noida" ); // test the access string subst = "Universe"; lbt[1] = subst; // access all the strings foreach ( string s in lbt ) { Console.WriteLine( "Value: {0}", s ); } } } } |
||||||||||||||
Output:
Value: Hello Value: Universe Value: www.csitquestions.com Value: Pvt Value: Ltd Value: Noida Value: Value: |
||||||||||||||
The program begins in Main( ), creating a new ListBoxTest object and passing two strings to the constructor. When the object is created, an array of Strings is created with enough room for eight strings. Four more strings are added using the Add method, and the second string is updated, just as in the previous example. | ||||||||||||||
The big change in this version of the program is that a foreach loop is called, retrieving each string in the listbox. The foreach loop automatically uses the IEnumerable<T> interface, invoking GetEnumerator( ). | ||||||||||||||
The GetEnumerator method is declared to return an IEnumerator of string: | ||||||||||||||
public IEnumerator<string> GetEnumerator( ) | ||||||||||||||
The implementation iterates through the array of strings, yielding each in turn: | ||||||||||||||
foreach ( string s in strings ) { yield return s; } |
||||||||||||||
Practical Example (Generics): | ||||||||||||||
testGenerics.cs | ||||||||||||||
using System; using System.Collections.Generic; using System.Text; namespace testGeneric { class Program { static void Main(string[] args) { // create arrays of int, double and char int[] intArray = { 1, 2, 3, 4, 5, 6 }; double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; char[] charArray = { 'H', 'E', 'L', 'L', 'O' }; Console.WriteLine( "Array intArray contains:" ); PrintArray( intArray ); // pass an int array argument Console.WriteLine( "Array doubleArray contains:" ); PrintArray( doubleArray ); // pass a double array argument Console.WriteLine( "Array charArray contains:" ); PrintArray( charArray ); // pass a char array argument Console.ReadLine(); } // output array of all types static void PrintArray< E >( E[] inputArray ) { foreach ( E element in inputArray ) Console.Write( element + " " ); Console.WriteLine( "\n" ); } // end method PrintArray } } |
||||||||||||||
The Output is: | ||||||||||||||
![]() |
||||||||||||||
To Download Generics example Click here. | ||||||||||||||
To view downloaded examples, Please Ensure that .NET Framework is installed on your system. | ||||||||||||||