Pallavi
Pallavi

Reputation: 1

how to create generic object for a class

How do I create a function which will accept generic object of class as an argument and will also have a generic return type?

Upvotes: 0

Views: 108

Answers (2)

Sam Holder
Sam Holder

Reputation: 32954

like this:

 public T Method<T>(T param)

the method may take as manay generic parameters as are needed to specify the types of the various input and return parameters, so you may also do this:

public T1 Method<T1,T2,T3>(T1 input1, T2 input2, T3 input3);

some information on generic methods on MSDN

Upvotes: 0

Jon Egerton
Jon Egerton

Reputation: 41589

If you want the same generic for the input and return types then:

public T DoSomething<T>(T input)

If you want different generics for the input and return types then:

public TReturn DoSomething<TInput, TReturn>(TInput input)

Upvotes: 2

Related Questions