Created
March 21, 2022 04:02
-
-
Save huanlin/575bc1f755ca4e5abe59b037dfd969f3 to your computer and use it in GitHub Desktop.
C# 10 String Interpolation Performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string Today() | |
{ | |
var d = DateTime.Now; | |
return $"今天是 {d.Year} 年 {d.Month} 月 {d.Day} 日"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C# 9 轉譯結果(使用 string.Format 方法) | |
public string Today() | |
{ | |
DateTime d = DateTime.Now; | |
return string.Format("今天是 {0} 年 {1} 月 {2} 日", d.Year, d.Month, d.Day); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// c# 10 轉譯結果 | |
public string Today() | |
{ | |
DateTime d = DateTime.Now; | |
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(12, 3); | |
defaultInterpolatedStringHandler.AppendLiteral("今天是 "); | |
defaultInterpolatedStringHandler.AppendFormatted(d.Year); | |
defaultInterpolatedStringHandler.AppendLiteral(" 年 "); | |
defaultInterpolatedStringHandler.AppendFormatted(d.Month); | |
defaultInterpolatedStringHandler.AppendLiteral(" 月 "); | |
defaultInterpolatedStringHandler.AppendFormatted(d.Day); | |
defaultInterpolatedStringHandler.AppendLiteral(" 日"); | |
return defaultInterpolatedStringHandler.ToStringAndClear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment