tags:

views:

269

answers:

7

(Note the code is an example)

I have the following syntax:

SomeMethod(() => x.Something)

What do the first brackets mean in the expression?

I'm also curious how you can get the property name from argument that is being passed in. Is this posssible?

+2  A: 

It's a lambda expression. That is, it's a way to create an anonymous function or delegate.

The general form is:

(input parameters) => expression

If you have

() => expression

then you have created a function that takes no arguments, and returns the result of the expression.

C# uses type inference to figure out what the types of the values are, and it captures local variables (like your "x" variable) by means of a lexical closure.

Daniel Pryden
+6  A: 

The () is an empty argument list. You're defining an anonymous function that takes no arguments and returns x.Something.

Edit: It differs from x => x.Something in that the latter requires an argument and Something is called on that argument. With the former version x has to exist somewhere outside the function and Something is called on that outside x. With the latter version there does not have to be an outside x and even if there is, Something is still called on the argument to the function and nothing else.

sepp2k
+1  A: 

the () mean that this method doesn't take any parameters. for example, if you assign a normal event handler using a lambda expression, it would look like this:

someButton.Click += (s, e) => DoSomething();
Botz3000
+2  A: 

I assume x is declared in somewhere inside your method, if yes, you can compare this lambda expression with a delegate that has no paramaters and return the type of x.someproperty

delegate{
 return x.someproperty;
}

that is the same as:

() => x.someproperty
Cleiton
The former won't compile.
Dykam
@Dykam, who said it wont? It will. try to compile this example: http://cleigomes.net/powersource/Output.aspx?fl=3w1e3djw.she
Cleiton
So it assumes a return type?
Arec Barrwin
In this example, yes, it does. As i said, the return type will be the type of "x.someProperty"
Cleiton
+6  A: 

What do the first brackets mean in the expression?

It's the lambda syntax for a method that takes no parameters. If it took 1 parameter, it'd be:

SomeMethod(x => x.Something);

If it took n + 1 arguments, then it'd be:

SomeMethod((x, y, ...) => x.Something);

I'm also curious how you can get the property name from argument that is being passed in. Is this possible?

If your SomeMethod takes an Expression<Func<T>>, then yes:

void SomeMethod<T>(Expression<Func<T>> e) {
    MemberExpression op = (MemberExpression)e.Body;
    Console.WriteLine(op.Member.Name);
}
Mark Brackett
A: 

To get the name of the property you need SomeMethod to have an argument of the type of System.Linq.Expressions.Expression<System.Func<object>>. You can then go through the expression to determine the property name.

MaLio
+1  A: 

See also the following two blog posts that discuss exactly your second question and provide alternative approaches:

How to Find Out Variable or Parameter Name in C#?

How to Get Parameter Name and Argument Value From C# Lambda via IL? (Or "How NOT to Use .NET Linq Expressions in Order to Get Parameter Name and Argument Value From C# Lambda?")

Christoph Rüegg