C# 6.0 特性学习
C# 6.0 学习
1. 属性可直接赋值 Public int X{ get; } = 5;
2. 静态引用:
using static System.Math;
…
public double Distance =>Sqrt(X * X + Y * Y);
…
3. 空操作符 ?.
…
if (temp != null)
temp.xx();
…
if (temp != null && temp.yy > 0)
…
now
…
temp?.xx();
…
if (temp?.yy > 0)
——————————————————————
this?.temp?.xx();
____________________________________________
var temp = this.object as Object;
if (temp != null)
temp.ToString();
…
(object as Object)?.ToString();
4. 字符串处理
$”{x}, {y}” === > String.Format(“{0}, {1}”,x, y);
5. 方法 属性 => 使用,只能写单行代码,多行直接使用原来形式就好。省去单行代码多余两行的括弧
6. nameof表达式
public void AddObject(Object object)
{
if (object == null)
throw new ArgumentNullException(“object”);
}
public void AddObject(Object object)
{
if (object == null)
throw new ArgumentNullException(nameof(object));
}
正常提示的object是手写字符串,使用nameof无论Object是什么对象都无需关心
7. 用this来调用其他的
public class PointTest
{
int X{ get;}
int Y{ get;}
public PointTest(int x, int y)
{
X = x;
Y = y;
}
public PointTest ():this(10, 10);
}
浙公网安备 33010602011771号