Instead of using {0} {1}
, etc. I want to use {title}
instead. Then fill that data in somehow (below I used a Dictionary
). This code is invalid and throws an exception. I wanted to know if i can do something similar to what i want. Using {0 .. N}
is not a problem. I was just curious.
Dictionary<string, string> d = new Dictionary<string, string>();
d["a"] = "he";
d["ba"] = "llo";
d["lol"] = "world";
string a = string.Format("{a}{ba}{lol}", d);
You can implement your own:
It's possible now
With Interpolated Strings of C# 6.0 you can do this:
A little more involved than the other extension method, but this should also allow non-string values and formatting patterns used on them, so in your original example:
Will also work...
Why a Dictionary? It's unnecessary and overly complicated. A simple 2 dimensional array of name/value pairs would work just as well:
Call the above with:
Results in:
"Dictionary = unnecessary (really, it's unnecessary)"