You must Sign In to post a response.
  • Category: .NET

    What is Generics & Collections

    Hi all,
    Can any one give clear idea about generics & collections in c#,
    what purpose we are using generics & collections ?,
    advantage and disadvantage of it ?

    Thanks in advance :)
  • #762740
    Hello Ramesh Kumar,

    Generics :

    Generic classes have type parameters. Separate classes, each with a different field type in them, can be replaced with a single generic class. The generic class introduces a type parameter. This becomes part of the class definition itself.

    Sample program :

    using System;

    class MyTest<T>
    T _value;

    public MyTest(T t)
    // The field has the same type as the parameter.
    this._value = t;

    public void Write()

    class Program
    static void Main()
    // Use the generic type Test with an int type parameter.
    MyTest<int> test1 = new MyTest<int>(5);
    // Call the Write method.

    // Use the generic type Test with a string type parameter.
    MyTest<string> test2 = new MyTest<string>("cat");

    Output :

    More reference of Generic Class :

    Collections :

    Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables.

    Sample program :

    static void Main(string[] args)
    //Defining an ArrayList
    ArrayList obj = new ArrayList();

    //Adding elements

    //Adding elements to specific position
    obj.Insert(2, "China");

    //Accessing elements
    for (int i = 0; i < obj.Count;i++ )
    Console.WriteLine("At Index["+i+"]= "+obj[i].ToString());

    Console.WriteLine("Press any key");

    More reference of Collections :

    Hope this will help you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762746

    The main difference between those 2 is Generics are Type Safe, where as Collection are not Type Safe. If you want to know more about the difference between those 2, you need to read few articles out of this,
    Refer below links ""

    Hope this will helpful to you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #762749

    Generics are same like c++ templates.
    1.When functionality of more than one method is same, instead of going for overloading methods we can write a single generic method which will work for all.
    2.One generic data type can be used to work with different data types.
    3.We can have generic classes, generic methods and generic data types.
    4.Generics are type safe.

    Collections like arrays and array list got their own advantages and disadvantages as well.
    1.Arrays are type safe(will allow only similar datatypes) and cannot grow size once declared.
    2.ArrayList are not type safe(allows dissimilar datatypes) but size can be grown.

    Where as generics are type safe and size can be grown.

    For more examples refer our DNS article in below link.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762772
    Hai Ramesh,
    First of all you need to know about collections- Collections are the ways to store multiple data in a single type like Array, ArrayList, Stack, Queue etc.
    So the collection represents the set of objects in which each object can be accessed through loop.
    Generics are the special type of collections.
    Collections are part of specif type like create a collection of integer then it will be
    int[] x={1,5,3,4,,7,7,7};
    but Generics can be created for the flexible type and type can be determined during runtime and not at the compile time.
    Also generics can be used with the the class, methods, properties etc.
    For more details on Generics, you can go through the below article:

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

Sign In to post your comments