If hasSecond == false
, I want to write
myString = string.Format(CultureInfo.InvariantCulture,
"{0}",
"first");
If hasSecond == true
, I want to write
myString = string.Format(CultureInfo.InvariantCulture,
"{0}{1}",
"first",
"second");
How can I combine them together?
For example, by using conditional expressions, the following doesn't seem work, because it doesn't make the fourth parameter optional:
myString = string.Format(CultureInfo.InvariantCulture,
"{0}" + (hasSecond == true ? "{1}" : string.Empty),
"first",
hasSecond == true ? "second" ; string.Empty);
when hasSecond==false
, it becomes
myString = string.Format(CultureInfo.InvariantCulture,
"{0}" + string.Empty,
"first",
string.Empty);
where we only have one place holder {0}
, while have two parameters "first"
and string.Empty
to fill it in.