C#知識系列
C# 6 多了FormattableString,這部分目前理解的部分有大兩大塊
1.針對文化產生結果的字串
2.可以進行做後續的參數化
來看第一個Sample來佐證
//依據文化特定字串來實作FormattableString
var cultures = new System.Globalization.CultureInfo[]
{
System.Globalization.CultureInfo.GetCultureInfo("en-US"),
System.Globalization.CultureInfo.GetCultureInfo("en-GB"),
System.Globalization.CultureInfo.GetCultureInfo("nl-NL"),
System.Globalization.CultureInfo.InvariantCulture
};
var date = DateTime.Now;
var number = 31_415_926.536;
FormattableString message = $"{date,20}{number,20:N3}";
foreach (var culture in cultures)
{
var cultureSpecificMessage = message.ToString(culture);
Console.WriteLine($"{culture.Name,-10}{cultureSpecificMessage}");
}
第二個例子,針對這樣特性可以進行參數化動作
//某些狀況可以參數化
var name = "Eddie";
FormattableString fmtString = $"hello {name}";
Console.WriteLine(fmtString.ArgumentCount); //1
Console.WriteLine(fmtString.Format); //hello {0}
foreach (var arg in fmtString.GetArguments())
{
Console.WriteLine(arg); //Eddie
Console.WriteLine(arg.GetType()); //System.String
}
元哥的筆記