What do two question marks together mean in C#?

前端 未结 18 1129
借酒劲吻你
借酒劲吻你 2020-11-22 03:41

Ran across this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

What do the two question marks mean, is it some ki

18条回答
  •  渐次进展
    2020-11-22 04:10

    Note:

    I have read whole this thread and many others but I can't find as thorough answer as this is.

    By which I completely understood the "why to use ?? and when to use ?? and how to use ??."

    Source:

    Windows communication foundation unleashed By Craig McMurtry ISBN 0-672-32948-4

    Nullable Value Types

    There are two common circumstances in which one would like to know whether a value has been assigned to an instance of a value type. The first is when the instance represents a value in a database. In such a case, one would like to be able to examine the instance to ascertain whether a value is indeed present in the database. The other circumstance, which is more pertinent to the subject matter of this book, is when the instance represents a data item received from some remote source. Again, one would like to determine from the instance whether a value for that data item was received.

    The .NET Framework 2.0 incorporates a generic type definition that provides for cases like these in which one wants to assign null to an instance of a value type, and test whether the value of the instance is null. That generic type definition is System.Nullable, which constrains the generic type arguments that may be substituted for T to value types. Instances of types constructed from System.Nullable can be assigned a value of null; indeed, their values are null by default. Thus, types constructed from System.Nullable may be referred to as nullable value types. System.Nullable has a property, Value, by which the value assigned to an instance of a type constructed from it can be obtained if the value of the instance is not null. Therefore, one can write:

    System.Nullable myNullableInteger = null;
    myNullableInteger = 1;
    if (myNullableInteger != null)
    {
    Console.WriteLine(myNullableInteger.Value);
    }
    

    The C# programming language provides an abbreviated syntax for declaring types constructed from System.Nullable. That syntax allows one to abbreviate:

    System.Nullable myNullableInteger;
    

    to

    int? myNullableInteger;
    

    The compiler will prevent one from attempting to assign the value of a nullable value type to an ordinary value type in this way:

    int? myNullableInteger = null;
    int myInteger = myNullableInteger;
    

    It prevents one from doing so because the nullable value type could have the value null, which it actually would have in this case, and that value cannot be assigned to an ordinary value type. Although the compiler would permit this code,

    int? myNullableInteger = null;
    int myInteger = myNullableInteger.Value;
    

    The second statement would cause an exception to be thrown because any attempt to access the System.Nullable.Value property is an invalid operation if the type constructed from System.Nullable has not been assigned a valid value of T, which has not happened in this case.

    Conclusion:

    One proper way to assign the value of a nullable value type to an ordinary value type is to use the System.Nullable.HasValue property to ascertain whether a valid value of T has been assigned to the nullable value type:

    int? myNullableInteger = null;
    if (myNullableInteger.HasValue)
    {
    int myInteger = myNullableInteger.Value;
    }
    

    Another option is to use this syntax:

    int? myNullableInteger = null;
    int myInteger = myNullableInteger ?? -1;
    

    By which the ordinary integer myInteger is assigned the value of the nullable integer "myNullableInteger" if the latter has been assigned a valid integer value; otherwise, myInteger is assigned the value of -1.

提交回复
热议问题