开发者

Not able to understand Generic Methods

I understand this statement :-

List<string> a;

which means that a can contain values of only string type.

But when used with methods i get confused. For eg.

public T1 methodName <T1,T2>(T1 t, T1 p)

I understand the method is returning object of type T1 and accepts objects of type T1. But what is the significance of <T1,T2>? Why is it required?

Edit :- Based on the answers i received, i guess <T1, T2> are there so that input arguments looks simila开发者_Go百科r. If <T1> was there then all input arguments must be of type T1 and if <T1,T2> is there means all arguments must be of type either T1 or T2

But what exactly is meant by this statement :-

public static TSummary Accumulate <TInput, TSummary> (IEnumerable <TInput> coll, Action <TInput, TSummary> action)

The definition says all input must be of either TInput or TSummary but the second argument is of type Action. So i am still confused.

Thanks in advance :)


If the method is really defined in the way that you've typed exactly:

public T1 methodName <T1, T2>(T1 t, T1 p)

...then T2 is unrelated to the argument types or return type. It might affect what's happening inside the method, however (e.g., the method may internally call typeof(T2) and do something with that).

If it was a typo, and the second parameter should read T2 p, then DaeMoohn is correct.


UPDATE:

If <T1> was there then all input arguments must be of type T1 and if <T1,T2> is there means all arguments must be of type either T1 or T2

No! Inaccurate!

The types of the parameters are specified right there in the parameter declaration: (T1 t, T1 p) -- both must be of type T1 (just like if the parameters were declared (int x, int y) they'd both have to be int).

Let's look at that Accumulate signature:

public static TSummary Accumulate <TInput, TSummary> (IEnumerable <TInput> coll, Action <TInput, TSummary> action)

This method takes some sequence of TInput values (coll) and some delegate pointing to a method that accepts a TInput and a TSummary parameter (action). The method returns an object of type TSummary.

What's confusing about your first example is that one of the generic type arguments, T2, just happened not to be anywhere within the method signature itself. This simply means that T2 is not related to either the parameters or the return value. But it could still be used within the method.

For example, consider this (fictional) method:

string GetTypeName<T>();

What does T mean in the above signature? It isn't the return type (string), nor is it in the parameter list (there are none). So does it mean anything? Presumably, it indicates what type you want to get the name of. So internally the method would probably look like this:

return typeof(T).FullName;

The point here is that T2 in your first example must (if anything) affect only the internals of the method itself. Again: it is not related to the parameters or the return value.


public T1 methodName <T1,T2>(T1 t, T1 p) lets you pass 2 generic parameters.
Hence, you can call this method in the following form

methodName<string, string>("hello", "world");
methodName<string, int>("hello", "world");
methodName<int, string>(1, 2);

Note: I didn't find the use of T2 & don't know, why is it required when the method signature isn't using it. Hence, I am passing all the parameters above in string form.

Simply put, it means that the method accepts 2 parameters which can be any type of your choice (unless the restrictions are placed).


The <T1,T2> section is to tell the compiler that T1 and T2 are type parameters. Simply starting their names with "T" isn't enough, that's just a convention. Once you've defined the type parameter names, you can use the names wherever you would use a type inside the method and the method signature.


But what exactly is meant by this statement :-

public static TSummary Accumulate <TInput, TSummary>(IEnumerable <TInput> coll, Action<TInput, TSummary> action)

The definition says all input must be of either TInput or TSummary but the second argument is of type Action. So i am still confused.

The second argument is of type Action<TInput, TSummary>, not Action.


It means methodName returns an object of type T1 and accepts two parameters of types T1 and T2.


In order to define the types of a generic method parameters like this you should use this T1,T2 notation.


You could get around the whole issue by making your program simpler. E.g. use a table of objects instead of generics in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜