??运算符,你是干嘛用的

?运算符,大家应该熟知,两个作用

1、三元运算符x==true?y=1:y=-1 

2、.Net里面定义空类型的一种表示 int?表示int型可以含空置

那么??干什么用的呢!!!

其实他是?运算符的综合体,我个人这么理解,也便于记忆。

MSDN上是这样说明的如果 ?? 运算符的左操作数非空,该运算符将返回左操作数,否则返回右操作数。

例子直接看MSDN上的吧,已经很清晰明了了

// nullable_type_operator.cs
using System;
class MainClass
{
    static int? GetNullableInt()
    {
        return null;
    }

    static string GetStringValue()
    {
        return null;
    }

    static void Main()
    {
        // ?? operator example.
        int? x = null;

        // y = x, unless x is null, in which case y = -1.
        int y = x ?? -1;

        // Assign i to return value of method, unless
        // return value is null, in which case assign
        // default value of int to i.
        int i = GetNullableInt() ?? default(int);

        string s = GetStringValue();
        // ?? also works with reference types. 
        // Display contents of s, unless s is null, 
        // in which case display "Unspecified".
        Console.WriteLine(s ?? "Unspecified");
    }
}

  

原文地址:https://www.cnblogs.com/diorlv/p/2284626.html