What is Generics in .NET Part - 1
Sunday, February 22, 2009
What is Generics ?
The name gives you little bit of idea that why it must be so lets start upon what are advantages of using generics while coding in .Net technology.
When we look at the term "generic", unrelated to the programming world, it simply means something that is not tied to any sort of brand name. For example, if we purchase some generic dish soap, soap that has no brand name on it, we know that we are buying dish soap and expect it to help us clean our dishes, but we really don't know what exact brand (if any) will be inside the bottle itself. We can treat it as dish soap even though we don't really have any idea of its exact contents.
Generics is to facilitate code reuse, especially the reuse of algorithms, C# includes a feature called generics. Just as methods are powerful because they can take parameters, classes that take type parameters have significantly more functionality as well, and this is what generics enable. Like their predecessor, templates, generics enable the definition of algorithms and pattern implementations once, rather than separately for each type. However, C# implements a type-safe version of templates that differs slightly in syntax and greatly in implementation from its predecessors in C++ and Java. Note that generics were added to the runtime and C# with version 2.0.
Think of Generics in this manner. We can refer to a class, where we don't force it to be related to any specific Type, but we can still perform work with it in a Type-Safe manner. A perfect example of where we would need Generics is in dealing with collections of items (integers, strings, Orders etc.). We can create a generic collection than can handle any Type in a generic and Type-Safe manner. For example, we can have a single array class that we can use to store a list of Users or even a list of Products, and when we actually use it, we will be able to access the items in the collection directly as a list of Users or Products, and not as objects (with boxing/unboxing, casting).
What is Generic type
Lets create a very simple generic class and demonstrate how it can be used as a container for a variety of other Types. Below is a simple example of what a generic class could look like:
public class Col
{
T t;
public T Val { get { return t; } set { t = value; } }
}
The class name "Col
What is Generic Collections
The .NET team has provided us with a number of generic collections to work with in the the latest version of the .NET Framework. List, Stack, and Queue are all going to be implemented for us. Let's see an example on how to use the provided Generic List class. Which will give Output as
public class User
{
protected string name;
protected int age;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
}
Above Class can be use as
public class UserData
{
public static void Main(string[] args)
{
System.Collections.Generic.List
for (int x = 0; x < user =" new" name = "Rob" age =" x;" x =" 0;">
Rob0:0
Rob1:1
Rob2:2
Rob3:3
Rob4:4
press enter
Rob0:0
Rob1:1
Rob2:2
Rob3:3
Rob4:4