C# language feature:

  • Generics

applies to

  • classes, interfaces

benefit

  • Facilitates code reuse, type safety, and performance. Generics are commonly used with collection classes.

how to use

  • 1) Insert <T> after class name and specify Type T where desired within class.
  • 2) Declare class object and specify desired type.

note

  • Multiple types may be specified e.g. <T1, T2>

example:

    class GenericList<objectType>{  // 1) Insert `<T>` after class name and
        public void Add(objectType listObject) { } // specify Type `T` where desired within class.
    }

    class Use_of_Generic_Class_Example
    {
        // 2) Declare class object and specify desired type i.e int.
        GenericList<int> NumberList; 
    }