Delegates and Generics in c#

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

Questions by mfrony

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.

Showing Answers 1 - 9 of 9 Answers

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.

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.

  Was this answer useful?  Yes

Adding on sutanu_halder answer:

1. Delegate executes asynchronousely, while Generice execute synchronousely.
2. Delegate is not type safe. We can define anonymous method which can execute without creating delegate object in memory. Generic is type-safe.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions