I have seen one example in a book but I would be a great help to see more examples.
I have MSDN link. if you guys have any links to discussion on Generics please post.
thanks
nath
Printable View
I have seen one example in a book but I would be a great help to see more examples.
I have MSDN link. if you guys have any links to discussion on Generics please post.
thanks
nath
http://www.developer.com/net/net/article.php/2229511
http://www.ondotnet.com/pub/a/dotnet...7/liberty.html
Are you looking to learn about it or do you just want links?
Thanks Mendhak.
I just started using Generics (BCL provided ones). But I looking forward to write my own Generic classes. The books I am referring to do not have good samples.
thanks
nath
Generics are actually dead easy once you understand the underlying concept, like so many things. When you declare a method you declare its arguments, which allow you to pass lots of different objects of the same type to that method. You are restricted to that type though. Generics allow you to use a variable argument of a variable type. Let's say that you had a method that returned the element of an array at a specific index. Without Generics you'd have to write a single, weakly-typed method:or else multiple overloads to cover every possible type you might want to use:Code:public object GetElementAtIndex(Array arr, int index)
{
return arr.GetValue(index);
}
With Generics you get the best of both worlds. You can write a single method but, when used it will be strongly-typed:Code:public string GetElementAtIndex(string[] arr, int index)
{
return arr[index];
}
public int GetElementAtIndex(int[] arr, int index)
{
return arr[index];
}
So that method takes an array of T and returns a T. Just like regular parameters, Generic type parameters are unknown until run time, but once they are they become fixed in one place they become fixed everywhere. That means that we don't know what the type of the array or the return type of the method are at design time, but we do know that they are the same because they are both T. Once you pass an array of a particular type, T becomes that type, which means that the return type of the method becomes that type as well. If T was used anywhere else in the method it would also become that type. As a result, if we pass an array of string to the method then the return type must be string. Likewise if we pass an array of int to the method then the return type must be int. In Generic language, if we pass an array of T to the method then the return type must be T, where T is a variable that represents any type. T is just another parameter, but it represents a type instead of a value.Code:public T GetElementAtIndex<T>(T[] arr, int index)
{
return arr[index];
}
The concept of generics is extended beyond simple methods like this to classes to, hence Generic collections. For example, the List<T> class. Within that class the T parameter is used in many places, as arguments to methods and return types. When you declare a List<T> variable and establish the type, e.g.that fixes every occurrence of T throughout the class definition to string.Code:List<string> myList = new List<string>;