以往在做比較簡單的字串串接,我們可能會用 + 運算符號進行串接,或是用 String.Format 帶入 Pattern 與要串接的字串去處理,像是下面這樣:
1 | ... |
在 C# 6.0 導入了 String Interpolation,可以像下面這樣簡化寫法:
1 | ... |
有點像是本來 String.Format 的 Pattern 內直接用斜線與大括號包住帶入運算式,且支援 optional alignment 與 format specifiers 的設定。
這邊來看個完整的使用範例:
1 | using System; |
運行結果如下:

反組譯看一下,其實背後也是將其編譯成 String.Format。
