c# - Calling a void-parameter lambda gives compiler error -


from question: lambda expression void input

i have following simple code:

        int minutes = () => 9; 

i compiler error:

cannot convert lambda expression type 'int' because not delegate type

i've found several questions error they're more specific issues. want give lambda body thought i'd start simple first check syntax:

//i know weird example int minutes = ()=> { if(x==9) return 9; else return 5;} 

from c# guide

a lambda expression block of code (an expression or statement block) treated object. can passed argument methods, , can returned method calls.

...

lambda expressions code can represented either delegate, or expression tree compiles delegate.

that means lambda expression can represented , can represent different things: delegate or expression tree.

your expression

() => 9; 

can many different things, example

public class c { delegate int intdelegate(); public void m() {     func<int> minutes = () => 9;     intdelegate minutes2 = () => 9;     expression<func<int>> minutesexpression = () => 9;     expression<intdelegate> minutesexpression2 = () => 9; } } 

so want use?

by way, because of that, can't use var them. , can see how c# compiler work under hood different lambda here-> lambda delegate , expression tree


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -