Lambda Expression issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey,

Am trying to write a lambda expression for a delegate, am getting an error as
------------------------------
only assignment, call, increment, decrement, and new expression can be used
as a statement
-----------------------------

For the following code.

// Delegate Declaration
delegate void dgSmp(string str);

//Lambda Expression
dgSmp dbObj = x => x + " Morning "; // Here throws the error
string strX = dbObj("Happy "); // expted value at strX is "Happy Morning"

------------------------------------------

Can some one help me to understand where i went wrong ..

Thanks
Ruth
 
Ruth,

Your delegate doesn't have a return type. You need to do the following:

delegate string dgSmp(string str);

dgSmp dbObj = x => x + " Morning "; // Here throws the error
string strX = dbObj("Happy "); // expted value at strX is "Happy Morning"
 
Hey,

Interpret the message by the compiler. That clearly states that you can do
all the possible activities, but you are not allowing the delegate to do
that. Your Lambda expression has some string value return.

The possible ways to resolve the issue is either rewrite your lambda
expression or change the signature of the deligate

HTH
 
Back
Top