Abbreviation when calling common methods in C #

If i have a method

void foo<T>(T bar){}

I can call it this way:

string s = string.Empty;
foo(s);

As I understand it, the compiler / runtime can infer a type,

However, if I change the method to this:

T foo<T,T2>(T2 bar){...}

Then I should call it "full", indicating the type of the input parameter and the type of the return value:

string s = string.Empty;
foo<int,string>(s);

Is there a way to shorten this, so I don't need to specify the type of input parameters? I.e.

foo<int>(s);

thank

+5
source share
2 answers

You can always rewrite your method to:

void foo<T, U>(U bar, out T baz)
{
    baz = default(T);
}

if you really need an output type ... Now:

string s = string.Empty;
int i;

foo(s, out i);

will work fine.

. , , !

EDIT: , ...

, ()?

... .

+3

, T . , .

# "" .

+3

All Articles