i want to return a string value from an anonymous method/delegate. How can i achieve something like this: Note this will give a compile time error: Cannot convert anonymous method to type 'string' because it is not a delegate type
StringBuilder sb = new StringBuilder("aaa");
sb.Replace("aaa", delegate()
{
return "bbb";
});
I' am also trying to create an extension method so i can write code like:
StringBuilder sb = new StringBuilder("aaa");
sb.Replace("aaa", () =>
{
return "bbb";
});
What would the signiture of the extension method be?
EDIT:
We have a large internal class which generates tedious xml files. We want to make the method more readable, more than anything else. So we want to go from:
StringBuilder sb = new StringBuilder(_repository.GetGenericXml1Template());
sb.Replace("$Placeholder", GetXml1Helper());
private void string GetXml1Helper()
{
StringBuilder sb = new StringBuilder(_repository.GetGenericXml2Template());
sb.replace("$Var1", DB.Var1);
....
return sb.ToString();
}
So rather than having dozens of helper methods doing slightly similar things we want to at least make the code more readble.
To:
StringBuilder sb = new StringBuilder(_repository.GetGenericXml1Template());
sb.Replace("$Placeholder", () =>
{
StringBuilder sb = new StringBuilder(_repository.GetGenericXml2Template());
sb.replace("$Var1", DB.Var1);
....
return sb.ToString();
}
);