tags:

views:

31

answers:

1

Hi buddies,

I had a thought on using the generic method in c# as like we do in c++.

Normally a method looks like this:

public static (void/int/string) methodname((datatype) partameter)
{
              return ...;
}

I had a thought whether can we implement the generics to this method like this:

public static <T> methodname(<T> partameter)
{
              return ...;
}

Using as a generic to define the datatype.

Can anyone pls suggest whether the above declaration is correct and can be used in c#?

Thanks in advance.

+1  A: 

Not quite like that, no. It would be:

public static T MethodName<T>(T parameter)
{
    ...
}

The <T> after MethodName shows that it's introducing a type parameter.

EDIT: As per the comment, you can't use this for a void method - you can't use void as a type argument, basically.

Jon Skeet
Also worth mentioning that this does not work for 'void' (first type in googler1's example).
Paolo Tedesco