Allow C# to infer generic type parameters from return type?

What is the thinking around letting C# infer generic type params when returning from a method? I'm sure I'm not explaining this right, so let me demonstrate:

Code Snippet

staticint Foo()

{

return Bar();

}

static T Bar<T>()

{

returndefault(T);

}

It seems to me that in this case, the compiler could infer that Foo's call to Bar needs to be Bar<int>, correct? Is there a reason it doesn't allow this?

Thanks,

Michael

[1095 byte] By [Michael_Giagnocavo] at [2008-3-6]
# 1

Hi,

This is an interesting approach. I think that if you allow that you may also allow:

int x = Bar();

and T again would be int.

The problems is see with that is what happens if you do:

IEnumerable ie = Bar();

Marcelo.

MarceloGuerra-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Visual C# Orcas...
# 2

I don't see the problem there; how is that different than doing:

IEnumerable ie = Bar<IEnumerable>();

?

Thanks,

Michael

Michael_Giagnocavo at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Visual C# Orcas...
# 3

You are right, my example was not good Smile.

I'll take note of your suggestion.

Marcelo.

MarceloGuerra-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Visual C# Orcas...

Visual Studio Orcas

Site Classified