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 similar. 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:
…then
T2is unrelated to the argument types or return type. It might affect what’s happening inside the method, however (e.g., the method may internally calltypeof(T2)and do something with that).If it was a typo, and the second parameter should read
T2 p, then DaeMoohn is correct.UPDATE:
No! Inaccurate!
The types of the parameters are specified right there in the parameter declaration:
(T1 t, T1 p)— both must be of typeT1(just like if the parameters were declared(int x, int y)they’d both have to beint).Let’s look at that
Accumulatesignature:This method takes some sequence of
TInputvalues (coll) and some delegate pointing to a method that accepts aTInputand aTSummaryparameter (action). The method returns an object of typeTSummary.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 thatT2is 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:
What does
Tmean 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:The point here is that
T2in 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.