前言
首先很抱歉,我们之前的维信小程序学习计划因为某些原因需要推迟了,但是很快就会回归啦。我们今天来学习一下C#的一些新特性。
$符号
$符号使得我们不必再输入冗余的占位符{0}、{1},而是直接在字符串中输入变量


静态引用
现在有个需求,需要你计算两个数的绝对值的和
以前的做法是使用Math.Abs方法,很容易可以实现

而一旦运算很多的时候,需要频繁地写Math。现在我们有了新的办法
引入命名空间:using static System.Math;于是有了

在前台调用一下: Console.WriteLine(StaticDemo.oldMethod(-1, -2));
Console.WriteLine(StaticDemo.newMethod(-1, -2));

如上图测试,两个方法的结果是一样的。
nameof
我们在网上提现,假如需要做一个提现数目不能大于100的验证,用于提示我们自己哪个字段出了错误
如下图所示:

假如count这个字段我们以后需要进行更改,那所有用到count这个字段的字符串都要更改,很麻烦,也很容易出错
此时,我们只需要:

这样子,不管变量的名字如何改变,任何用到此变量名字的字符串都将更改,nameof不仅可以适用于变量,还可以用于类、属性、方法等。
Null表达式
定义一个数组,我们来循环遍历出每一项的长度
旧的方法是使用三元表达式(运算符),如下图所示

新方法:使用??和?.
要点如注释所示

正如我们预算的那样,输出结果如下图所示:

欢迎关注我的微信公众号:dotNET学习天地 --------获取更多学习资源