#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)
        {
            thePile.Add(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.

 

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,028 – Generic Types vs. Generic Methods

  1. Pingback: Dew Drop – February 7, 2014 (#1718) | Morning Dew

Leave a comment