I have the following method:
private byte[] GetEmailAsBytes(string lstrBody)
{
byte[] lbytBody;
ASCIIEncoding lASCIIEncoding = new ASCIIEncoding();
lbytBody = lASCIIEncoding.GetBytes(lstrBody);
return lbytBody;
}
I was wondering if this could be converted to a lambda expression. Im new to this. I have tried:
Func<string> BodyToBytes = x => {
ASCIIEncoding lASCIIEncoding = new ASCIIEncoding();
return lASCIIEncoding.GetBytes(x);
}
but this does not compile. Simply i wish to convert a string to a series of bytes, and for interest sake would like to do this using lambda expressions.
The expression
Func<string>is equivalent to a function which accepts no arguments and returns astring.Your example clearly returns a
byte[], but you want it to accept astringand return abyte[].To solve this, change
BodyToBytesto match the following. Note that the type of the arguments come first, comma delimited, followed by the return type. In this case,xwill be of typestring.For a reference, see Func Type or the MSDN docs.