generics in c#

21
 How to: Create an Iterator Block for a Generic List (C# Progr amming Guide) Visual Studio 2010 In this example the generic class Stack<T> implements the generic inter face IEnumerator <T>. An array of the type T is declared and assigned values by using the method Push. In the GetEnumerator method, the values of the array are returned using the yield return statement. The non-generic GetEnumerator is also implemented, because IEnumerable<T> inherits from IEnumerable. This example shows the typical implementation, in which the non-generic method  just forwards the call to the generic method. Example using System.Collections; using System.Collections.Generic; namespace GenericIteratorExample { public class Stack<T> : IEnumerable<T> { private T[] values = new T[100]; private int top = 0; public void Push(T t) { values[top++] = t; } public T Pop() { return values[--top]; } // These make Stack<T> implement IEnumerable<T> allowing // a stack to be used in a foreach statement. public IEnumerator<T> GetEnumerator() { for (int i = top - 1; i >= 0; i-- ) { yield return values[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } // Iterate from top to bottom. public IEnumerable<T> TopToBottom { get {

Upload: jewelmir

Post on 02-Nov-2015

35 views

Category:

Documents


2 download

DESCRIPTION

How to use generic classes in C#

TRANSCRIPT

  • How to: Create an Iterator Block for aGeneric List (C# Programming Guide)Visual Studio 2010

    In this example the generic class Stack implements the generic interface IEnumerator.An array of the type T is declared and assigned values by using the method Push. In theGetEnumerator method, the values of the array are returned using the yield return statement.

    The non-generic GetEnumerator is also implemented, because IEnumerable inherits fromIEnumerable. This example shows the typical implementation, in which the non-generic methodjust forwards the call to the generic method.

    Example

    using System.Collections;using System.Collections.Generic;

    namespace GenericIteratorExample{

    public class Stack : IEnumerable{

    private T[] values = new T[100];private int top = 0;

    public void Push(T t) { values[top++] = t; }public T Pop() { return values[--top]; }

    // These make Stack implement IEnumerable allowing// a stack to be used in a foreach statement.public IEnumerator GetEnumerator(){

    for (int i = top - 1; i >= 0; i-- ){

    yield return values[i];}

    }

    IEnumerator IEnumerable.GetEnumerator(){

    return GetEnumerator();}

    // Iterate from top to bottom.public IEnumerable TopToBottom{

    get{

  • // Since we implement IEnumerable// and the default iteration is top to bottom,// just return the object.return this;

    }}

    // Iterate from bottom to top.public IEnumerable BottomToTop{

    get{

    for (int i = 0; i < top; i++){

    yield return values[i];}

    }}

    // A parameterized iterator that return n items from the toppublic IEnumerable TopN(int n){

    // in this example we return less than N if necessaryint j = n >= top ? 0 : top - n;

    for (int i = top; --i >= j; ){

    yield return values[i];}

    }}

    // This code uses a stack and the TopToBottom and BottomToTop properties// to enumerate the elements of the stack.class Test{

    static void Main(){

    Stack s = new Stack();for (int i = 0; i < 10; i++){

    s.Push(i);}

    // Prints: 9 8 7 6 5 4 3 2 1 0// Foreach legal since s implements IEnumerableforeach (int n in s){

    System.Console.Write("{0} ", n);}System.Console.WriteLine();

    // Prints: 9 8 7 6 5 4 3 2 1 0// Foreach legal since s.TopToBottom returns IEnumerableforeach (int n in s.TopToBottom){

    System.Console.Write("{0} ", n);

  • }System.Console.WriteLine();

    // Prints: 0 1 2 3 4 5 6 7 8 9// Foreach legal since s.BottomToTop returns IEnumerableforeach (int n in s.BottomToTop){

    System.Console.Write("{0} ", n);}System.Console.WriteLine();

    // Prints: 9 8 7 6 5 4 3// Foreach legal since s.TopN returns IEnumerableforeach (int n in s.TopN(7)){

    System.Console.Write("{0} ", n);}System.Console.WriteLine();

    // Keep the console window open in debug mode.System.Console.WriteLine("Press any key to exit.");System.Console.ReadKey();

    }}

    }/* Output:

    9 8 7 6 5 4 3 2 1 09 8 7 6 5 4 3 2 1 00 1 2 3 4 5 6 7 8 99 8 7 6 5 4 3

    */

  • Introduction to Generics (C# ProgrammingGuide)Visual Studio 2013

    Generic classes and methods combine reusability, type safety and efficiency in a way that theirnon-generic counterparts cannot. Generics are most frequently used with collections and themethods that operate on them. Version 2.0 of the .NET Framework class library provides a newnamespace, System.Collections.Generic, which contains several new generic-based collectionclasses. It is recommended that all applications that target the .NET Framework 2.0 and later usethe new generic collection classes instead of the older non-generic counterparts such asArrayList. For more information, see Generics in the .NET Framework Class Library (C#Programming Guide).

    Of course, you can also create custom generic types and methods to provide your owngeneralized solutions and design patterns that are type-safe and efficient. The following codeexample shows a simple generic linked-list class for demonstration purposes. (In most cases, youshould use the List class provided by the .NET Framework class library instead of creatingyour own.) The type parameter T is used in several locations where a concrete type wouldordinarily be used to indicate the type of the item stored in the list. It is used in the followingways:

    As the type of a method parameter in the AddHead method. As the return type of the public method GetNext and the Data property in the nested Node

    class. As the type of the private member data in the nested class.

    Note that T is available to the nested Node class. When GenericList is instantiated with aconcrete type, for example as a GenericList, each occurrence of T will be replaced with int.

    // type parameter T in angle bracketspublic class GenericList{

    // The nested class is also generic on T.private class Node{

    // T used in non-generic constructor.public Node(T t){

    next = null;data = t;

    }

    private Node next;public Node Next{

    get { return next; }set { next = value; }

  • }// T as private member data type.private T data;

    // T as return type of property.public T Data{

    get { return data; }set { data = value; }

    }}

    private Node head;

    // constructorpublic GenericList(){

    head = null;}

    // T as method parameter type:public void AddHead(T t){

    Node n = new Node(t);n.Next = head;head = n;

    }

    public IEnumerator GetEnumerator(){

    Node current = head;

    while (current != null){

    yield return current.Data;current = current.Next;

    }}

    }

    The following code example shows how client code uses the generic GenericList class tocreate a list of integers. Simply by changing the type argument, the following code could easilybe modified to create lists of strings or any other custom type:

    class TestGenericList{

    static void Main(){

    // int is the type argumentGenericList list = new GenericList();

    for (int x = 0; x < 10; x++){

    list.AddHead(x);}

  • foreach (int i in list){

    System.Console.Write(i + " ");}System.Console.WriteLine("\nDone");

    }}

  • Generic Classes (C# Programming Guide)Visual Studio 2013

    Generic classes encapsulate operations that are not specific to a particular data type. The mostcommon use for generic classes is with collections like linked lists, hash tables, stacks, queues,trees, and so on. Operations such as adding and removing items from the collection areperformed in basically the same way regardless of the type of data being stored.

    For most scenarios that require collection classes, the recommended approach is to use the onesprovided in the .NET Framework class library. For more information about using these classes,see Generics in the .NET Framework Class Library (C# Programming Guide).

    Typically, you create generic classes by starting with an existing concrete class, and changingtypes into type parameters one at a time until you reach the optimal balance of generalization andusability. When creating your own generic classes, important considerations include thefollowing:

    Which types to generalize into type parameters.

    As a rule, the more types you can parameterize, the more flexible and reusable your codebecomes. However, too much generalization can create code that is difficult for otherdevelopers to read or understand.

    What constraints, if any, to apply to the type parameters.

    A good rule is to apply the maximum constraints possible that will still let you handle thetypes you must handle. For example, if you know that your generic class is intended for useonly with reference types, apply the class constraint. That will prevent unintended use ofyour class with value types, and will enable you to use the as operator on T, and check fornull values.

    Whether to factor generic behavior into base classes and subclasses.

    Because generic classes can serve as base classes, the same design considerations apply hereas with non-generic classes. See the rules about inheriting from generic base classes later inthis topic.

    Whether to implement one or more generic interfaces.

    For example, if you are designing a class that will be used to create items in a generics-basedcollection, you may have to implement an interface such as IComparable where T is thetype of your class.

  • The rules for type parameters and constraints have several implications for generic classbehavior, especially regarding inheritance and member accessibility. Before proceeding, youshould understand some terms. For a generic class Node, client code can reference the classeither by specifying a type argument, to create a closed constructed type (Node).Alternatively, it can leave the type parameter unspecified, for example when you specify ageneric base class, to create an open constructed type (Node). Generic classes can inheritfrom concrete, closed constructed, or open constructed base classes:

    class BaseNode { }class BaseNodeGeneric { }

    // concrete typeclass NodeConcrete : BaseNode { }

    //closed constructed typeclass NodeClosed : BaseNodeGeneric { }

    //open constructed typeclass NodeOpen : BaseNodeGeneric { }

    Non-generic, in other words, concrete, classes can inherit from closed constructed base classes,but not from open constructed classes or from type parameters because there is no way at runtime for client code to supply the type argument required to instantiate the base class.

    //No errorclass Node1 : BaseNodeGeneric { }

    //Generates an error//class Node2 : BaseNodeGeneric {}

    //Generates an error//class Node3 : T {}

    Generic classes that inherit from open constructed types must supply type arguments for anybase class type parameters that are not shared by the inheriting class, as demonstrated in thefollowing code:

    class BaseNodeMultiple { }

    //No errorclass Node4 : BaseNodeMultiple { }

    //No errorclass Node5 : BaseNodeMultiple { }

    //Generates an error//class Node6 : BaseNodeMultiple {}

    Generic classes that inherit from open constructed types must specify constraints that are asuperset of, or imply, the constraints on the base type:

    class NodeItem where T : System.IComparable, new() { }

  • class SpecialNodeItem : NodeItem where T : System.IComparable, new(){ }

    Generic types can use multiple type parameters and constraints, as follows:

    class SuperKeyTypewhere U : System.IComparablewhere V : new()

    { }

    Open constructed and closed constructed types can be used as method parameters:

    void Swap(List list1, List list2){

    //code to swap items}

    void Swap(List list1, List list2){

    //code to swap items}

    If a generic class implements an interface, all instances of that class can be cast to that interface.

    Generic classes are invariant. In other words, if an input parameter specifies a List,you will get a compile-time error if you try to provide a List.

  • Constraints on Type Parameters (C#Programming Guide)Visual Studio 2013

    When you define a generic class, you can apply restrictions to the kinds of types that client codecan use for type arguments when it instantiates your class. If client code tries to instantiate yourclass by using a type that is not allowed by a constraint, the result is a compile-time error. Theserestrictions are called constraints. Constraints are specified by using the where contextualkeyword. The following table lists the six types of constraints:

    Constraint Description

    where T: struct The type argument must be a value type. Any value type except Nullablecan be specified.

    where T : class The type argument must be a reference type; this applies also to any class,interface, delegate, or array type.

    where T : new()The type argument must have a public parameterless constructor. Whenused together with other constraints, the new() constraint must be specifiedlast.

    where T : The type argument must be or derive from the specified base class.

    where T :

    The type argument must be or implement the specified interface. Multipleinterface constraints can be specified. The constraining interface can also begeneric.

    where T : U The type argument supplied for T must be or derive from the argumentsupplied for U.

    Why Use Constraints

    If you want to examine an item in a generic list to determine whether it is valid or to compare itto some other item, the compiler must have some guarantee that the operator or method it has tocall will be supported by any type argument that might be specified by client code. Thisguarantee is obtained by applying one or more constraints to your generic class definition. Forexample, the base class constraint tells the compiler that only objects of this type or derived fromthis type will be used as type arguments. Once the compiler has this guarantee, it can allowmethods of that type to be called in the generic class. Constraints are applied by using thecontextual keyword where. The following code example demonstrates the functionality we canadd to the GenericList class by applying a base class constraint.

  • public class Employee{

    private string name;private int id;

    public Employee(string s, int i){

    name = s;id = i;

    }

    public string Name{

    get { return name; }set { name = value; }

    }

    public int ID{

    get { return id; }set { id = value; }

    }}

    public class GenericList where T : Employee{

    private class Node{

    private Node next;private T data;

    public Node(T t){

    next = null;data = t;

    }

    public Node Next{

    get { return next; }set { next = value; }

    }

    public T Data{

    get { return data; }set { data = value; }

    }}

    private Node head;

    public GenericList() //constructor{

    head = null;}

  • public void AddHead(T t){

    Node n = new Node(t);n.Next = head;head = n;

    }

    public IEnumerator GetEnumerator(){

    Node current = head;

    while (current != null){

    yield return current.Data;current = current.Next;

    }}

    public T FindFirstOccurrence(string s){

    Node current = head;T t = null;

    while (current != null){

    //The constraint enables access to the Name property.if (current.Data.Name == s){

    t = current.Data;break;

    }else{

    current = current.Next;}

    }return t;

    }}

    The constraint enables the generic class to use the Employee.Name property because all items oftype T are guaranteed to be either an Employee object or an object that inherits from Employee.

    Multiple constraints can be applied to the same type parameter, and the constraints themselvescan be generic types, as follows:

    class EmployeeList where T : Employee, IEmployee, System.IComparable,new(){

    // ...}

    By constraining the type parameter, you increase the number of allowable operations and methodcalls to those supported by the constraining type and all types in its inheritance hierarchy.

  • Therefore, when you design generic classes or methods, if you will be performing any operationon the generic members beyond simple assignment or calling any methods not supported bySystem.Object, you will have to apply constraints to the type parameter.

    When applying the where T : class constraint, avoid the == and != operators on the typeparameter because these operators will test for reference identity only, not for value equality.This is the case even if these operators are overloaded in a type that is used as an argument. Thefollowing code illustrates this point; the output is false even though the String class overloads the== operator.

    public static void OpTest(T s, T t) where T : class{

    System.Console.WriteLine(s == t);}static void Main(){

    string s1 = "target";System.Text.StringBuilder sb = new System.Text.StringBuilder("target");string s2 = sb.ToString();OpTest(s1, s2);

    }

    The reason for this behavior is that, at compile time, the compiler only knows that T is areference type, and therefore must use the default operators that are valid for all reference types.If you must test for value equality, the recommended way is to also apply the where T :IComparable constraint and implement that interface in any class that will be used toconstruct the generic class.

    Constraining Multiple Parameters

    You can apply constraints to multiple parameters, and multiple constraints to a single parameter,as shown in the following example:

    class Base { }class Test

    where U : structwhere T : Base, new() { }

    Unbounded Type Parameters

    Type parameters that have no constraints, such as T in public class SampleClass{}, arecalled unbounded type parameters. Unbounded type parameters have the following rules:

  • The != and == operators cannot be used because there is no guarantee that the concrete typeargument will support these operators.

    They can be converted to and from System.Object or explicitly converted to any interfacetype.

    You can compare to null. If an unbounded parameter is compared to null, the comparison willalways return false if the type argument is a value type.

    Type Parameters as Constraints

    The use of a generic type parameter as a constraint is useful when a member function with itsown type parameter has to constrain that parameter to the type parameter of the containing type,as shown in the following example:

    class List{

    void Add(List items) where U : T {/*...*/}}

    In the previous example, T is a type constraint in the context of the Add method, and anunbounded type parameter in the context of the List class.

    Type parameters can also be used as constraints in generic class definitions. Note that the typeparameter must be declared within the angle brackets together with any other type parameters:

    //Type parameter V is used as a type constraint.public class SampleClass where T : V { }

    The usefulness of type parameters as constraints with generic classes is very limited because thecompiler can assume nothing about the type parameter except that it derives fromSystem.Object. Use type parameters as constraints on generic classes in scenarios in which youwant to enforce an inheritance relationship between two type parameters.

  • Generic Interfaces (C# Programming Guide)Visual Studio 2013

    It is often useful to define interfaces either for generic collection classes, or for the genericclasses that represent items in the collection. The preference for generic classes is to use genericinterfaces, such as IComparable rather than IComparable, in order to avoid boxing andunboxing operations on value types. The .NET Framework class library defines several genericinterfaces for use with the collection classes in the System.Collections.Generic namespace.

    When an interface is specified as a constraint on a type parameter, only types that implement theinterface can be used. The following code example shows a SortedList class that derivesfrom the GenericList class. For more information. SortedList adds the constraint whereT : IComparable. This enables the BubbleSort method in SortedList to use the genericCompareTo method on list elements. In this example, list elements are a simple class, Person,that implements IComparable.

    //Type parameter T in angle brackets.public class GenericList : System.Collections.Generic.IEnumerable{

    protected Node head;protected Node current = null;

    // Nested class is also generic on Tprotected class Node{

    public Node next;private T data; //T as private member datatype

    public Node(T t) //T used in non-generic constructor{

    next = null;data = t;

    }

    public Node Next{

    get { return next; }set { next = value; }

    }

    public T Data //T as return type of property{

    get { return data; }set { data = value; }

    }}

    public GenericList() //constructor{

  • head = null;}

    public void AddHead(T t) //T as method parameter type{

    Node n = new Node(t);n.Next = head;head = n;

    }

    // Implementation of the iteratorpublic System.Collections.Generic.IEnumerator GetEnumerator(){

    Node current = head;while (current != null){

    yield return current.Data;current = current.Next;

    }}

    // IEnumerable inherits from IEnumerable, therefore this class// must implement both the generic and non-generic versions of// GetEnumerator. In most cases, the non-generic method can// simply call the generic method.System.Collections.IEnumerator

    System.Collections.IEnumerable.GetEnumerator(){

    return GetEnumerator();}

    }

    public class SortedList : GenericList where T : System.IComparable{

    // A simple, unoptimized sort algorithm that// orders list elements from lowest to highest:

    public void BubbleSort(){

    if (null == head || null == head.Next){

    return;}bool swapped;

    do{

    Node previous = null;Node current = head;swapped = false;

    while (current.next != null){

    // Because we need to call this method, the SortedList// class is constrained on IEnumerableif (current.Data.CompareTo(current.next.Data) > 0){

  • Node tmp = current.next;current.next = current.next.next;tmp.next = current;

    if (previous == null){

    head = tmp;}else{

    previous.next = tmp;}previous = tmp;swapped = true;

    }else{

    previous = current;current = current.next;

    }}

    } while (swapped);}

    }

    // A simple class that implements IComparable using itself as the// type argument. This is a common design pattern in objects that// are stored in generic lists.public class Person : System.IComparable{

    string name;int age;

    public Person(string s, int i){

    name = s;age = i;

    }

    // This will cause list elements to be sorted on age values.public int CompareTo(Person p){

    return age - p.age;}

    public override string ToString(){

    return name + ":" + age;}

    // Must implement Equals.public bool Equals(Person p){

    return (this.age == p.age);}

    }

  • class Program{

    static void Main(){

    //Declare and instantiate a new generic SortedList class.//Person is the type argument.SortedList list = new SortedList();

    //Create name and age values to initialize Person objects.string[] names = new string[]{

    "Franscoise","Bill","Li","Sandra","Gunnar","Alok","Hiroyuki","Maria","Alessandro","Raul"

    };

    int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };

    //Populate the list.for (int x = 0; x < 10; x++){

    list.AddHead(new Person(names[x], ages[x]));}

    //Print out unsorted list.foreach (Person p in list){

    System.Console.WriteLine(p.ToString());}System.Console.WriteLine("Done with unsorted list");

    //Sort the list.list.BubbleSort();

    //Print out sorted list.foreach (Person p in list){

    System.Console.WriteLine(p.ToString());}System.Console.WriteLine("Done with sorted list");

    }}

    Multiple interfaces can be specified as constraints on a single type, as follows:

    class Stack where T : System.IComparable, IEnumerable{}

  • An interface can define more than one type parameter, as follows:

    interface IDictionary{}

    The rules of inheritance that apply to classes also apply to interfaces:

    interface IMonth { }

    interface IJanuary : IMonth { } //No errorinterface IFebruary : IMonth { } //No errorinterface IMarch : IMonth { } //No error//interface IApril : IMonth {} //Error

    Generic interfaces can inherit from non-generic interfaces if the generic interface is contra-variant, which means it only uses its type parameter as a return value. In the .NET Frameworkclass library, IEnumerable inherits from IEnumerable because IEnumerable only uses Tin the return value of GetEnumerator and in the Current property getter.

    Concrete classes can implement closed constructed interfaces, as follows:

    interface IBaseInterface { }

    class SampleClass : IBaseInterface { }

    Generic classes can implement generic interfaces or closed constructed interfaces as long as theclass parameter list supplies all arguments required by the interface, as follows:

    interface IBaseInterface1 { }interface IBaseInterface2 { }

    class SampleClass1 : IBaseInterface1 { } //No errorclass SampleClass2 : IBaseInterface2 { } //No error

    The rules that control method overloading are the same for methods within generic classes,generic structs, or generic interfaces. For more information.

  • Generic Methods (C# Programming Guide)Visual Studio 2013

    A generic method is a method that is declared with type parameters, as follows:

    static void Swap(ref T lhs, ref T rhs){

    T temp;temp = lhs;lhs = rhs;rhs = temp;

    }

    The following code example shows one way to call the method by using int for the typeargument:

    public static void TestSwap(){

    int a = 1;int b = 2;

    Swap(ref a, ref b);System.Console.WriteLine(a + " " + b);

    }

    You can also omit the type argument and the compiler will infer it. The following call to Swap isequivalent to the previous call:

    Swap(ref a, ref b);

    The same rules for type inference apply to static methods and instance methods. The compilercan infer the type parameters based on the method arguments you pass in; it cannot infer the typeparameters only from a constraint or return value. Therefore type inference does not work withmethods that have no parameters. Type inference occurs at compile time before the compilertries to resolve overloaded method signatures. The compiler applies type inference logic to allgeneric methods that share the same name. In the overload resolution step, the compiler includesonly those generic methods on which type inference succeeded.

    Within a generic class, non-generic methods can access the class-level type parameters, asfollows:

    class SampleClass{

    void Swap(ref T lhs, ref T rhs) { }}

  • If you define a generic method that takes the same type parameters as the containing class, thecompiler generates warning CS0693 because within the method scope, the argument supplied forthe inner T hides the argument supplied for the outer T. If you require the flexibility of calling ageneric class method with type arguments other than the ones provided when the class wasinstantiated, consider providing another identifier for the type parameter of the method, as shownin GenericList2 in the following example.

    class GenericList{

    // CS0693void SampleMethod() { }

    }

    class GenericList2{

    //No warningvoid SampleMethod() { }

    }

    Use constraints to enable more specialized operations on type parameters in methods. Thisversion of Swap, now named SwapIfGreater, can only be used with type arguments thatimplement IComparable.

    void SwapIfGreater(ref T lhs, ref T rhs) where T : System.IComparable{

    T temp;if (lhs.CompareTo(rhs) > 0){

    temp = lhs;lhs = rhs;rhs = temp;

    }}

    Generic methods can be overloaded on several type parameters. For example, the followingmethods can all be located in the same class:

    void DoWork() { }void DoWork() { }void DoWork() { }