Replace all the occurrence of a string I have the following function to replace all the occurrence of a string that matches certain token.

public string ReplaceTokenBySample(string StingValue)
{
    List&lt;Token&gt; tokens = GetTokenList();
    foreach (var token in tokens)
    {
        StingValue = StingValue.Replace(token.Token, token.SampleValue);
    }
    return StingValue;
}


GetTokenList(); will return

Token                   SampleValue 
##Username##            John Doe    
##UserEmail##           john.doe@domain.com 
##UserFirstName##       John    
##UserLastName##        Doe


How can I optimize this code?

Full console app code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Token
    {
        public string TokenValue { get; set; }
        public string SampleValue { get; set; }
    }
    public class Program
    {
        public List&lt;Token&gt;GetTokenList()
        {
            List&lt;Token&gt; tokens = new List&lt;Token&gt;
            {
                new Token(){ TokenValue = "##Username##", SampleValue="John Doe" },
                new Token(){ TokenValue = "##UserEmail##", SampleValue="john.doe@domain.com " },
                new Token(){ TokenValue = "##UserFirstName##", SampleValue="John" },
                new Token(){ TokenValue = "##UserLastName##", SampleValue="Doe"}
            };
            return tokens;
        }

        public string ReplaceTokenBySample(string StringValue)
        {
            List&lt;Token&gt; tokens = GetTokenList();
            foreach (var token in tokens)
            {
                StringValue = StringValue.Replace(token.TokenValue, token.SampleValue);
            }
            return StringValue;
        }

        public static void Main(string[] args)
        {
            Program obj = new Program();
            string StringValue ="Hello ##Username##! I have emailed you at ##UserEmail##. ##UserFirstName## ##UserLastName## how is you days going on. Have a good day ##Username##. ";
            Console.WriteLine(obj.ReplaceTokenBySample(StringValue));
        }
    }
}
If this is your real use-case then I'd not go beyond the obvious optimizations. In this case you're creating a new string for each replacement, you can use StringBuilder.Replace() instead:

public string ReplaceTokenBySample(string value)
{
    var result = new StringBuilder(value);
    GetTokenList().ForEach(x =&gt; result.Replace(x.TokenValue, x.SampleValue));

    return result.ToString();
}


If instead of List&lt;Token&gt; you have IEnumerable&lt;Token&gt; (or if you want to avoid List&lt;T&gt;.ForEach()) then you can use:

public string ReplaceTokenBySample(string value)
{
    GetTokenList().Aggregate(new StringBuilder(value),
        (result, item) =&gt; result.Replace(item.TokenValue, item.SampleValue));

    return result.ToString();
}


Few things to consider:


You're performing ordinal case-sensitive comparison, it seems to be appropriate to replace placeholders with their actual value and it has the benefit to be reasonably fast. If this is not the case then you should definitely go with t3schb0t's answer (which handles this properly and it's incredibly more readable.)
Parameters should be camelCase.
It's usually confusing to overwrite parameters value with something else (especially for non trivial code), compiler is able to optimize your code to get the best of it even when using a local variable.


Few more notes about overall design: you do not validate your parameters anywhere, if they're only used internally to your assembly then mark them internal or private (as appropriate) and ASSERT about their content (for example: why do you declare Token as public?)

Names sound little bit strange, you do not need to repeat the type in the name: TokenSample and TokenValue inside the Token class (and StringValue for a value of type String).

If you intend to fill Token programmatically then adding a Token(string, string) ctor may help to keep your code shorter.