<p>Is there any way to format a string by name rather than position in C#?</p>

<p>In python, I can do something like this example (shamelessly stolen from <a href="http://docs.python.org/lib/typesseq-strings.html" rel="noreferrer">here</a>):</p>

<pre><code>&gt;&gt;&gt; print '%(language)s has %(#)03d quote types.' % \
      {'language': "Python", "#": 2}
Python has 002 quote types.
</code></pre>

<p>Is there any way to do this in C#?  Say for instance:</p>

<pre><code>String.Format("{some_variable}: {some_other_variable}", ...);
</code></pre>

<p>Being able to do this using a variable name would be nice, but a dictionary is acceptable too.</p>

I think the closest you'll get is an indexed format:
```
```
String.Format("{0} has {1} quote types.", "C#", "1");
```
```
There's also String.Replace(), if you're willing to do it in multiple steps and take it on faith that you won't find your 'variables' anywhere else in the string:
```
```
string MyString = "{language} has {n} quote types.";
MyString = MyString.Replace("{language}", "C#").Replace("{n}", "1");
```
```
Expanding this to use a List:
```
```
List<KeyValuePair<string, string>> replacements = GetFormatDictionary();  
foreach (KeyValuePair<string, string> item in replacements)
{
    MyString = MyString.Replace(item.Key, item.Value);
}
```
```
You could do that with a Dictionary<string, string> too by iterating it's .Keys collections, but by using a List<KeyValuePair<string, string>> we can take advantage of the List's .ForEach() method and condense it back to a one-liner:
```
```
replacements.ForEach(delegate(KeyValuePair<string,string>) item) { MyString = MyString.Replace(item.Key, item.Value);});
```
```
A lambda would be even simpler, but I'm still on .Net 2.0.  Also note that the .Replace() performance isn't stellar when used iteratively, since strings in .Net are immutable.  Also, this requires the
```
MyString
```
variable be defined in such a way that it's accessible to the delegate, so it's not perfect yet.