C# 6 字符串插值(使用前缀$)

C# 6 引入了字符串前缀$的字符串插值。

string s = “hello”;

string y = $"{s} world";

等同于使用Format方法:

string y = string.Format("{0} world",s);

并且我们可以调用值的方法,如:

string y = $"{s.ToLower()} world";

使用新的字符串格式代码可读性要好一些如:

            int a = 1;
            int b = 2;
            string c = $"{a} + {b} = {a + b}";//使用$
            string d = string.Format("{0} + {1} = {2}", a, b, a + b);//使用Format

参考文章
https://blog.csdn.net/xc917563264/article/details/79348233

猜你喜欢

转载自blog.csdn.net/codingriver/article/details/83378514