views:

50

answers:

2

Hi! Can you please help me here, why the compiler does not infer the lambda input parameter right?

Example:

void Test(Action<string> n)
{
}
void Test(Action<int,string> n)
{
}

Ok so when doing this:

obj.Test(x=>{}); // compiler doesn't know x is a string

If I do this:

obj.Test((x,y)=>{}); // that works, compiler know x is a int and y is a string

Looks like I will have to specify the input parameter type (?)

obj.Test((string x) => {}) // <-- Prefer not doing this

So any reasons why the compiler can't get the type right?

Thanks! Carlos

A: 

Looking at your screenshot, the problem is quite simple. Your method name Test is the same as the enclosing class, Test. This is not allowed, and in fact, you should receive the compiler error:

'Test': member names cannot be the same as their enclosing type

Kirk Woll
Sorry about that, I wrote that very quickly, look at this example with correct names: http://img195.imageshack.us/img195/6766/overrb.png
carlosdubusm
@carlosdubusm, Hmm, doesn't happen for me: http://img835.imageshack.us/img835/9323/60539152.png (using vs2008 now too)
Kirk Woll
@carlosdubusm, to shed some more light on the problem, try invoking one of the string methods that *should* be there, and then compile it and see what the compiler error is.
Kirk Woll
Yeah, calling x.Length compiled correctly, but still the properties are not shown in the autocomplete dialog, so the problem is in my VS2008 right?
carlosdubusm
@carlosdubusm, do you have any plugins installed? And I assume you've already tried shutting down VS and restarting?
Kirk Woll
A: 

I just tried to recreate the problem with Visual Studio 2008, and did a screen capture here: http://screencast.com/t/YTAwNmQ4M

I was not able to reproduce the problem. I have seen Visual Studio do things like this in the past, but building always works, and often it is a memory issue. Closing Visual Studio, along with other programs that might be taking a lot of memory, and then starting Visual Studio back up has often helped.

So, basically, my answer is, "did you try turning on off, and then back on again?"

NerdFury
Looks like my VS is corrupted, when I place the mouse over the parameter it does says (parameter) string x, but the autocomplete desn't show the string methods and properties, ahh, I don't want to reinstall...
carlosdubusm
Restarted the machine and still same problem, well thank you anyway. I want to set the correct answer to both of you but that's not possible i think.
carlosdubusm