|
-
Aug 20th, 2006, 11:09 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] links for Generics..any please
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
-
Aug 21st, 2006, 10:07 AM
#2
Re: links for Generics..any please
-
Aug 21st, 2006, 12:54 PM
#3
Thread Starter
Hyperactive Member
Re: links for Generics..any please
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
-
Aug 21st, 2006, 06:01 PM
#4
Re: links for Generics..any please
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:
Code:
public object GetElementAtIndex(Array arr, int index)
{
return arr.GetValue(index);
}
or else multiple overloads to cover every possible type you might want to use:
Code:
public string GetElementAtIndex(string[] arr, int index)
{
return arr[index];
}
public int GetElementAtIndex(int[] arr, int index)
{
return arr[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 T GetElementAtIndex<T>(T[] 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.
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.
Code:
List<string> myList = new List<string>;
that fixes every occurrence of T throughout the class definition to string.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|