Tooobey
Tooobey

Reputation: 43

What's the datatype of lambda-expressions?

i have a small question i just can't figure out myself, what is the datatype of a lambda-expression like this: x => x>0
I hope you can help me.

Upvotes: 2

Views: 828

Answers (2)

Renat Zamaletdinov
Renat Zamaletdinov

Reputation: 1232

It's just a Func<T, bool> where T is type of x.

In general, Func<T, TResult> is the one of predefined delegate types for a method that accepts one argument of type T (x in your case) and returns some value of the type TResult (bool in your case)

More info here

Upvotes: 5

Corlet Dragos
Corlet Dragos

Reputation: 101

Lambda expressions don't have a definite type. The types that a lambda expression can be implicitly converted to is either delegate or Expression type.

You can find more information here

"Note that lambda expressions in themselves do not have a type because the common type system has no intrinsic concept of "lambda expression." However, it is sometimes convenient to speak informally of the "type" of a lambda expression. In these cases the type refers to the delegate type or Expression type to which the lambda expression is converted."

For your specific example some of the types to which the lambda can be assigned are:

  Predicate<int> predicate = x => x > 0;
  Func<int, bool> func = x => x > 0;
  Expression<Func<int, bool>> expression = x => x > 0;

Upvotes: 4

Related Questions