Lambda Expression issue

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
 
N

Nicholas Paldino [.NET/C# MVP]

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"
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top