Results 1 to 4 of 4

Thread: [RESOLVED] links for Generics..any please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Resolved [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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: links for Generics..any please


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width