.net 程序中几种常见的语法

在阅读代码的时候经常会看到几种奇怪的语法 这边来看看

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";
      }

    }



给类或者命名空间以及各别名

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值