Skip to content

Generics

What

It is a Class for List of Object.

Example for List of Integer without using Generics

public class IntList
    {
        public void Add(int number)
        {
            throw new NotImplementedException;
        }

        public int this[int index]
        {
            get
            {
                throw new NotImplementedException;
            }
        }
    }
public class ObjectList
    {
        public void Add(object number)
        {
            throw new NotImplementedException;
        }

        public int this[object index]
        {
            get
            {
                throw new NotImplementedException;
            }
        }
    }

Before Generics this Class Need Be created for every Object.

public class GenericList<T>
    {
        public void Add(T value)
        {
            throw new NotImplementedException();
        }
        public T this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
        }
    }

Generics let use Create List of Object without any performance paneity.