Skip to main content

C# Advanced Topics - Generics

Generics 


Before we start learning What Generics is ?  , let me explain what problem generics came to solve .

In .NET version 1 , we don't have generics .So ,lets say we need to create a list to store a list of numbers . The implementation of that class looks like below : 




Currently , as I am not concentrating on how data structures works , just assumed a sample list class with  only Add method , which add the passed integer into its list. Rather than defining all the methods like Remove , find and properties and functionality and so on , just planned to have my class and projected on how generics came into existence .

Let me define a simple class object here : 



Now , if i plan to store a book to the list defined above it doesn't work as int is a value type and our book class will be a reference type . So , I am forced to create some class like below, which will give the ability to add a list of books : 



So , the code is not reusable , so one solution to fix this is we can go with Object , which is parent class for every class in .NET.

But this too has an issue and that is  performance . The value type like int uses boxing (while stored) and unboxing (accessed) every time and for reference type it uses casting to perform on the class . To solve this issue is where generics came into action . 

Generic usage is something which looks like below : 

So , here for T we can pass any type of class i.e even a value type(int) or a reference type(book) or another data types . So generics is a class defined once and reused .This is the basics of creating a generic list . In practical terms most often you will be using generics as opposed to creating ones. 


Most often we use the one the generics list offered in .NET . Some of such predefined generic list in .NET are Dictionary , LinkedList and some interfaces like IEnumerable , IList , IDictionary and so on. 

So , this is the history of generics , but in the next section we can see how we can create a generic in real time if the application you are working on forces you to do so . 



Comments

Post a Comment

Popular posts from this blog

C# Advanced Topics - Lambda Expression

Lambda Expression  So what is a lambda expression and why are these lambda expression used for ?  As every article of me says you what and why , here as well we are going to get an answer for it .  A lambda expression is nothing but an anonymous method .   Oh really !! But what is anonymous method here ? It might be the next question which strikes you . Just follow through get an answer for it .  An Anonymous method is a method with no access modifier , no name and no return statement . But apart from this book definition we can look its snippet for a better understanding. Before that why do we use them ? Any guess ?  For convenience . We can write less code and achieve the same thing plus our code will be more readable readable . For our practical knowledge , lets take an example , lets say we would like to write a method which takes a number and returns cube of the number . Typically , this is how we do that   Now with lambda expression w...

C# Advanced Topics - Delegates

Delegates So what is a delegate ?  and Why does it come into existence ? A delegate is an object which knows how to call a method(or a group of methods). Simpler way to define is, delegate is a reference or a pointer to a function .  Now why do we need like that ? We can call methods directly , why do we need a delegate that is calling methods for us . Well this technique is helpful for us in designing extensible and flexible applications.  If we are asked to create a framework for image processing , So it looks some thing like this  So ,if I release this framework,the client or developer who is consuming the framework can use this as below and it might look as below  Now if one of the developer  wanted to apply another filter to resize the image , you need to create that filter and  re-compile and re-deploy both ImageFilter and ImageProcessor classes. Now imagine every time a developer in the world want a new filter you need to re-compile and and re-...