Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some trick to use 'out' parameters inside lambda function? [duplicate]

Tags:

c#

lambda

if ( (new Func</*out*/ string, bool>( (/*out*/ string uname) => ....

more details : that is a part of login function and I just want that my lambda function to changes login-name user with a out parameter and said me that user logined with it's bool return.

I really understand that I can return the Tuple and then get my string value but I want exactly out parameter for some personal clarity. I better return only string with null if user is not login, just want to know if I can use out parameters inside lambda functions.

And I really get that the code with expressions on the statement places is not so clean But none said me if that is really bad for compiler.

like image 443
cnd Avatar asked Apr 04 '11 14:04

cnd


1 Answers

Lambda expressions won't work, but for delegates you should be fine using a statement body:

bool outval = false; // definite assignment
Func<bool> func = () => {
    return SomeMethod(out foo);
};
bool returned = func();
// check both outval and returned

For delegates... You will need to define your own:

public delegate bool MyType(out string value);
like image 168
Marc Gravell Avatar answered Oct 27 '22 09:10

Marc Gravell