在阅读代码的时候经常会看到几种奇怪的语法 这边来看看
1.非空类型
int x=null;
int? y=null;
这边第一句会报错 而第二句不会 对于一些程序里值类型进行空赋值的时候经常使用,如下代用也是一样的效果
Nullable<int> t=null;
2.Global ::
这个用来覆盖全局命名空间,也用隐藏更贴切
如果我们把命名空间为 System那么调用Sytem前面就要Golbal
namespace System
{
class Program
{
static void Main(string[] args)
{
global::System.String ac = new String('c', 1);
}
}
}
3.default
如果你不知道给一个值赋值为神马类型的时候那么可以使用这个
给个例子
DateTime dt = default(DateTime);
4.??
是否为空值 如果是那么就用后面的值
例子
int? x = null;
int y = x ?? 2;
这边y=2
这代码相当于
int y=x==nll?x:2
5.using别名引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyProfb = MyPro;
namespace demo
{
class Program
{
static void Main(string[] args)
{
MyProfb proFb = new MyProfb();
proFb.print();
}
}
}
class MyPro
{
public string print()
{
return "HelloWorld";
}
}
给类或者命名空间以及各别名