Submitted Questions

  • delegates and Generics in c#

    What is the advantage of using delegates and Generics in c# .

    Star Read Best Answer

    Editorial / Best Answer

    sutanu_halder  

    • Member Since Dec-2007 | Apr 18th, 2008


    Advantage of Generic:-

    1. Generics provide type safety without the overhead of multiple implementations.
    Ex. We can create a linked list of string.
        LinkedList<string>linkList=new LinkedList<string>();
    There is no need to inherit from a base type and override members.The linked list is ready

    for immediate use.

    2. There is no need to write code to test for the correct data type because it is enforced

    at compile time. The need for type casting and the possibility of run-time errors are

    reduced.

    3. Generic collection types generally perform better for storing and manipulating value

    types because there is no need to box the value types.

    4. Generic delegates enable type-safe callbacks without the need to create multiple delegate

    classes.

    5.Generic delegates can also be used in dynamically generated code without requiring the

    generation of a delegate type. This increases the number of scenarios in which you can use

    lightweight dynamic methods instead of generating entire assemblies.

    Advantage of Delegate:

    Delegates are managed function pointers. they are type checked and held in spaces that can

    be reclaimed by the memory manager.