tags:

views:

147

answers:

4

When i need to pass a Generic Type I can use the Syntax

(Example : Ofcourse it is not a Generic Method)

public void Sample(T someValue)
{
  ......
}

What is the benefit of declaring Sample<T> ?

I mean

public void Sample<T> (T someValue)
{
    ......
}
+4  A: 

For this to work:

public void Sample(T someValue)
{
  ......
}

The type T has to be declared in the system already. And the method will only accept the type T or its derivatives.

By declaring this:

public void Sample<T> (T someValue)
{
    ......
}

you say the method will accept any type that comes.

Developer Art
Do you mean class instead of system?
jfar
Not necessarily. Can be declared in this class or elsewhere, just needs to be known at this place.
Developer Art
+1  A: 

You can use the 2nd example if your class isn't generic. It means you can make just that method generic. If your class is generic, you should use your 1st example.

JonoW
+12  A: 

Generic types and generic methods are very different things. It sounds like you have a generic type:

class SomeType<T> {
    public void Sample(T someValue) {...}
}

and are discussing having a generic method inside it:

class SomeType<T> {
    public void Sample<T>(T someValue) {...}
}

These are very different. In the first, inside Sample, then T means "the T that got passed to SomeType<T>". In the second, inside Sample, this is a separate and independent T - "the T that got passed to Sample<T>". In fact, calling it T (in this case) is a mistake. You could have, for example:

var obj = new SomeType<int>(); // here T for SomeType<T> is int
obj.Sample<decimal>(123.45M); // here T for Sample<T> is decimal

Note that there is no easy way (within Sample<T>) of saying "the T in SomeType<T>" - hence why you should rename the method's generic type parameter.

There are valid scenarios for this type of scenario (generic methods on generic types), for example (and note the new name):

class SomeType<T> {
    public void Sample<TActual>(TActual someValue) where TActual : T, new() {...}
}

This allows us to do some very interesting things in terms of inheritance, etc - or you might want a generic method that has little or no relation to T. That is fine too.

Marc Gravell
+4  A: 

Consider the following:

class SomeClass<T>
{
    public void Sample(T value)
    {
        // code goes here
    }
}

or this:

class SomeClass
{
    public void Sample<T>(T value)
    {
        // code goes here
    }
}

In the first case, for all calls to Sample in a specific instance T will be the same type. In the second case each call in a specific instance can have its own type of T, since the generic type argument is supplied with the method call.

The second approach can have many uses, but one that I have used myself sometimes is in factory methods:

public static class SomeFactory
{
    public static T CreateSomeObject<T>()
    {
        T result = Activator.CreateInstance<T>();
        // perform any extra initialization
        return result;
    }
}
Fredrik Mörk