tags:

views:

236

answers:

7

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();
}
);
A: 

This is the correct syntax to return a string form an anonymous method. Your problem is different - you're trying to pass an anonymous method to StringBuilder.Replace() as the second argument, where it expects a string.

Why are you doing this anyway?

ripper234
+1  A: 

Note this will give a compile time error: Cannot convert anonymous method to type 'string' because it is not a delegate type

Your delegate is fine – the error just says that it cannot be used here because Replace expects a string as the second argument.

What would the signiture of the extension method be?

Well, something like this:

static void Replace(this StringBuilder sb, Func<string> f);

– but why would you want this? What use-case does it fulfill?

Konrad Rudolph
We have a large class creates xml on the fly. Our xml files are deeply nested, so instead of creating dozens of little helper methods i thought i try with anonymous method to make it read alot better. The delegates will represent another nested xml level. The replace methods are replacing $Placeholders in the xml template file with values from the db
A: 

I believe that you need to extend the existing method.

Kindly refer to this link --> http://msdn.microsoft.com/en-us/library/bb383977.aspx

solairaja
A: 

You could do the following:

StringBuilder sb = new StringBuilder();
Func<string> func = delegate()
{
    return "HELLO";
};
sb.Replace("", func());

Although I don't see the use of this...

Maximilian Mayerl
You are missing the point, he wants to create an extension method that allows this, his anonymous method was fine.
Yannick M.
Well, he wrote that he wanted to create an "extension method too", so I thought that was just some kind of second question.
Maximilian Mayerl
A: 

Instead of a StringBuilder.Replace you may want to look into Regex.Replace. That way you can do somthing like:

Regex.Replace("Input string", "aaa", match => "bbb");

Very similar to what you want to do, just out the box.

Geoff
A: 

It is a little unclear how you're going to use this - since the example you've posted is trivial, I have to ask ts there any particular reason it needs to be an anonymous method? Couldn't it just be a regular instance method instead?

Martin Clarke
A: 

You have to use extension method to achive the expected..

since the default StringBuilder.replace doesnt accept Func<>, you have to create the extension method replace which meets your requirement

here the sample code.

public static class ExtensionMethods
{
          public static StringBuilder  replace(this StringBuilder Sb1,string input, 
          Func<string> anonymos)
          {
               return Sb1.Replace(input, anonymos.Invoke());
          }
}

and you have to invoke the extension replace method like this

        StringBuilder s1 = new StringBuilder();
        s1.Append("hai");
        s1.replace("hai", () =>
        {
            return "bb";
        });

hope this helps.

Cheers

Ramesh Vel

Ramesh Vel