c#中这样一个判断条件:if (IsTa ?? false)
我试了一下只有IsTa 为true的情况下,才会执行这个分支,这是什么意思?

解决方案 »

  1.   

    ?? Operator (C# Reference)The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.class NullCoalesce
    {
        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");
        }
    }参考:
    http://msdn.microsoft.com/en-us/library/ms173224.aspx
      

  2.   

    a = b ?? c
    等同于
    if(b == null)
        a = c;
    else
        a = b;
      

  3.   

    IsTa ?? false的意思是如果IsTa为null值,则该表达式的结果就是false,如果不为null,则使用IsTa的值作为表达式的结果。1. IsTa 为null时,IsTa ?? false ---》 false
    2. IsTa 为false时,IsTa ?? false ---》 false
    3. IsTa为true时,IsTa ?? false的结果就是IsTa的值,即为true,所以会去执行{}中的内容
      

  4.   

    IsTa ?? false
    IsTa为null时,返回false;不为null时,返回IsTa 
      

  5.   

    object isnull=null;isnull??"show"
    等价于
    isnull==null?"show":isnull