Can anyone tell me the difference between
public T Get<T>(int id)
and
public T Get(int id)
Can anyone tell me the difference between
public T Get<T>(int id)
and
public T Get(int id)
Compare:
class First
{
public T Get<T>(int id) // T is declared in the method scope
{
}
}
and
class Second<T>
{
public T Get(int id) // T is declared in the class scope
{
}
}
Also there is a 3rd scenario:
class Third<U>
{
public T Get<T>(int id) // T is declared in the method scope when class scope has another generic argument declared
{
}
}
The difference is that you use the first type of declaration if this is when T hasn't been defined before. ie.
public class MyClass
{
public T Get<T>(int id);
}
And the second when T has already been defined at the class level. ie.
public class MyClass<T>
{
public T Get(int id);
}
In this case you can also use the first type of declaration - this is effectively shorthand. There is no difference in the effect.
Edit In fact, the second declaration only requires that T be in scope, another example would be a nested class as in...
public class MyClass<T>
{
public class NestedClass
{
public T Get(int i);
}
}