Shortcut syntax for no-arg lambdas?

In 2.0, it was not possible to point a delegate to a property getter. There were workarounds, but none of them particularly elegant.

Although that restriction doesn't appear to be lifted in c# 3.0, one new type-safe workaround is to use a lambda with no arguments:

Code Snippet

var foo = new {Name=""};
foo.Name = "a";
Func<string> getName = ()=> foo.Name;

Console.WriteLine(getName());
foo.Name = "b";
Console.WriteLine(getName());


This is big improvement on what was possible in 2.0 - though I'm curious if there is a shorter syntax for a no-arg lambda expression (instead of the mildly hilarious syntax above).

Thanks,
- John

[840 byte] By [JohnSpurlock] at [2008-2-22]
# 1

Hi,

As far as I know there is no special syntax for non-arg lambdas beside the one you used in your code.

I never thought about other ways of looking at lamda syntax, but now that you show this I like this one:

Func<int, int> i = d=> d;

Regards,

Marcelo.

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

Visual Studio Orcas

Site Classified