#1,063 – Explicit Conversions and Type Parameters

You can’t explicitly convert a type parameter to either a reference type or a value type.  Casts to reference or value types are disallowed at compile-time.  The compiler allows casting the type parameter to an interface type and this conversion will succeed at run-time if the object implements that interface.

        public class ThingContainer<T>
            private T thing;

            public void SetThing(T t)
                thing = t;

                // Won't compile
                int i = (int)t;

                // Won't compile
                Dog d = (Dog)t;

                // Will compile, but throw
                // InvalidCastException at run-time
                // if T doesn't implement IBark
                IBark ib = (IBark)t;

        static void Main(string[] args)
            ThingContainer<Dog> dogcont = new ThingContainer<Dog>();
            dogcont.SetThing(new Dog("Bowser"));

            ThingContainer<Cow> cowcont = new ThingContainer<Cow>();
            cowcont.SetThing(new Cow("Bessie"));


#1,042 – Deriving from a Self-Referencing Constructed Type, Part I

When defining a type, the type can derive from a constructed generic type.

public class DogList : List<string>

You can use the name of the type being defined as the type argument to the type (or interface) that you’re deriving from.

Below, the Dog type is an argument to the IComparable generic interface.  We’re saying that we want Dog to implement IComparable<T> and it makes sense that the argument should be Dog.

    public class Dog : IComparable<Dog>
        public string Name { get; set; }
        public int WoofPower { get; set; }

        public Dog(string name, int woofPower)
            Name = name;
            WoofPower = woofPower;

        public int CompareTo(Dog other)
            // Returns
            //   < 0 if this < other
            //   0 is this == other
            //     > 0 if this > other
            return this.WoofPower - other.WoofPower;

Using Dog:

            Dog fido = new Dog("Fido", 4);
            Dog bowser = new Dog("Bowser", 9);

            int compare = fido.CompareTo(bowser);

#1,039 – Deriving from a Generic Class

A generic class includes one or more type parameters, allowing you to later declare a variable using a constructed type by specifying type arguments for each type parameter.

If you have an existing generic class, you can define a new class that inherits from the generic class.

Assume that we have a List<T> type, i.e. we can construct the type as follows:

            List<int> someInts = new List<int>();

We can also define a new class that inherits from List<T> and makes use of its type parameter as follows:

    public class ListPlus<T> : List<T>
        public void AddTwoItems(T thing1, T thing2)

We can now construct the new type by providing a type argument.

            ListPlus<Dog> dogs = new ListPlus<Dog>();
            Dog fido = new Dog("Fido");
            Dog bowser = new Dog("Bowser");
            dogs.AddTwoItems(fido, bowser);

#1,032 – Requiring Generic Type Parameters to Be a Reference or Value Type

By default, when you specify a type parameter in a generic class, the type can be any .NET type.  There are times, however, when you’d like to constrain the type in some ways, for example requiring that the type be either a reference type or a value type.

To require a type argument to represent a reference type, use the class keyword:

    public class PileOf<T> where T : class

To require a type argument to represent a non-nullable value type, use the struct keyword:

    public class PileOf<T> where T : struct

You may want to use one of these constraints when your generic type includes code that will only work on a reference type or on a value type.

#1,028 – Generic Types vs. Generic Methods

generic type is a type that uses a type parameter in its definition, which must be concretely specified when constructing the type.

    public class PileOf<T>
        private List<T> thePile;

        public PileOf()
            thePile = new List<T>();

        public void AddItem(T thing)

A generic method is a method that introduces a generic type parameter.

        static void Swap<T>(ref T item1, ref T item2)
            T temp = item1;
            item1 = item2;
            item2 = temp;

Methods within generic types that use the type parameter from the type are not consider generic.  The AddItem method in the PileOf<T> class above is not considered generic.  To be considered a generic method, the method must introduce a new type parameter not present as a type parameter in the containing class.


#326 – Generic Type vs. Constructed Type

Once a generic type is provided with type arguments, it is known as a constructed type.

Here is the definition of a generic type:

    // A generic type
    public class ThingContainer<TThing1, TThing2>
        public TThing1 Thing1;
        public TThing2 Thing2;

You declare instances of the generic type by providing arguments for its type parameters.  The type name with the arguments is the constructed type.

            // ThingContainer<Dog, DateTime> is a constructed type
            ThingContainer<Dog, DateTime> container = new ThingContainer<Dog, DateTime>();

            container.Thing1 = new Dog("Bob");
            container.Thing2 = DateTime.Now;