Many times when we talk about generics, we only understand List <>. But generics are totally depending on <T>. The feature of generics incorporated in Framework 2.0 enhances significant performance improvement and reusability of objects.
Features of Generic
- Type Safe: Generics are type safe (Strongly Type). You can store any type of data in that.
In the above example I have defined list of string type and as you can see I can only inserted string value into it.

- Performance: No doubt the performance is faster. To get into the performance you need to understand the concept of Boxing and Unboxing. When a value type is converted into object is called Boxing. And the vice versa is called unboxing. When you add something into non generic collection you need add everything as an Object.
Try this:

Same exists for the other non-generic type collection i.e. HashTable, Stacks etc.
- Reusability: Well yes generics are reusable objects. “Define and use” concepts are applicable on generics.
We can define Class, Method, Delegate, Interface and Events as Generics. Going further we will concentrate on this.
- Generic Class: The best part of generics is its ability and its usage. Below I have defined a generic class that allows me to pass any type in the constructor.
//We have defined class as Generic type public class GenericClassExample < T > { T AnyVariable; //We can pass any type in constructor by defining class as generic <T> GenericClassExample(T anyVariable) { AnyVariable = anyVariable; } public void PrintAnyVariable() { Console.WriteLine("Any Variable " + AnyVariable); } } - Generic Method: Same as the class, a method can also be declared as Generic type. Please find below the example for the same:
public class GenericMethodExample { public void GenericMethod < T > (T a, T b) { T val1, val2; val1 = a; val2 = b; } } - Generic Interface: Below is an example for the same:
namespace Project1 { public interface InterfaceGeneric < T > { void ShowNumber(T item); } } class Interface < T >: InterfaceGeneric < T > { public void ShowNumber(T item) { Console.WriteLine(item); Console.ReadKey(); } } class test { public static void Main(string[] args) { Interface < int > t1 = newInterface < int > (); Interface < string > t2 = newInterface < string > (); t1.ShowNumber(4); t2.ShowNumber("shubham"); } } - Generic Delegates: Here you go.
public class Program { delegate T GenericDel < T > (T n1, T n2); static void Main(string[] args) { GenericDel < int > objDelegate = newGenericDel < int > (Add); GenericDel < int > objDelegateMul = newGenericDel < int > (Multiply); Console.WriteLine(objDelegate(1, 2)); Console.WriteLine(objDelegateMul(1, 2)); Console.ReadLine(); } public static int Add(int a, int b) { return a + b; } public static int Multiply(int a, int b) { return a * b; } }
Hope this will make the concept of <T> clear to the readers. And event is homework for you all.

Art BergquistPosted Nov 30, 2019, 6:19 PM
I recommend replacing the period in "Here you go." with a colon (":").
Art BergquistPosted Nov 30, 2019, 6:18 PM
I recommend replacing "Below I have defined a generic class that allows me to pass any type in the constructor." with "Below, I have defined a generic class that allows me to pass any type into the constructor:"
Art BergquistPosted Nov 30, 2019, 6:18 PM
I recommend inserting a comma immediately after "further" in " Going further we will concentrate on this."
Art BergquistPosted Nov 30, 2019, 6:16 PM
I recommend replacing "Well yes generics are reusable objects." with "Well, yes, generics are reusable objects."
Art BergquistPosted Nov 30, 2019, 6:15 PM
I recommend replacing "Same exists for the other non-generic type collection i.e. HashTable, Stacks etc." with "Same exists for the other non-generic type collection (e.g., HashTable, Stacks, etc.)".
Art BergquistPosted Nov 30, 2019, 6:15 PM
I recommend replacing "When you add something into non generic collection you need add everything as an Object." with "When you add something into a non-generic collection, you need to add everything as an Object."
Art BergquistPosted Nov 30, 2019, 6:14 PM
I recommend replacing "When a value type is converted into object is called Boxing." with "When a value type is converted into an object, it is called Boxing."
Art BergquistPosted Nov 30, 2019, 6:13 PM
I recommend inserting a comma immediately after "performance" in "To get into the performance you need to understand the concept of Boxing and Unboxing."
Art BergquistPosted Nov 30, 2019, 6:12 PM
I recommend replacing "In the above example I have defined list of string type and as you can see I can only inserted string value into it." with "In the following example, I have defined a list of string type and, as you can see, I can only inserted string value into it."
Art BergquistPosted Nov 30, 2019, 6:11 PM
In the following line: "Type Safe: Generics are type safe (Strongly Type). You can store any type of data in that.", I recommend replacing "Type Safe" with "Type-Safe", "type safe" with "type-safe" and "(Strongly Type)" with "(strongly typed)".
Art BergquistPosted Nov 30, 2019, 6:10 PM
Thanks, Nishant! I see a few grammatical opportunites to improve your web page.
RAVI TPosted May 17, 2017, 9:13 AM
Very simple & clear. Thanks :)
Piyush RamtekePosted Apr 25, 2017, 2:46 AM
You have made the concept of <T> very clear by this article. Thank you.. !!!
scropio gurlPosted Jun 29, 2016, 2:21 AM
Too complicated. still not understand
Nishant MittalPosted May 23, 2016, 1:21 AM
Thanks
Humayun Kabir MamunPosted May 22, 2016, 7:58 AM
Nice...