C# .Net Understanding The Basic of Generic (Class and Method)

generic-cover

Hi Guys.. I know some of you already use .Net languages such as VB, C#, F#, or etc. But have you already known about the Generic? have you already utilized the Generic? if you’re not, that means you have to know about this right know. This new namespace already introduced on Version 2.0 of the .NET Framework, it has many advantages once you use it, those are reusability and efficiency.  With Generic we can use data type as parameter, as for example you can send parameter with data type int, string, object and other data types, then Generic will understand your parameter as we specify when we call Generic class or method. Generic is consist of Generic class and method. Maybe we already utilized Generic in ArrayList but we don’t realize that we already used Generic class 😀 , and now it’s time to get better knowledge.

Generic class is the class that has type parameter, if we have many classes that  have different data type, we can replace those classes with single Generic class.

C# console program codes :

Generic method is the method that has type parameter. Same with Generic class but we utilize Generic on method.

C# console program codes :

Both codes above will get this result after you run this program :

generic-class-result

Finally.. we have known about the Generic. That’s amazing right? Another thing is we can utilize Generic in some patterns such as Generic Repository pattern, Factory pattern, or etc.

FatkhanFauzi About FatkhanFauzi

A passionate software engineer who wants to share anything that he learns and some of his life experiences. Want to know more about him? see Fatkhan Fauzi Profile


So, what do you think ?

Leave a Reply

%d bloggers like this: