What do two question marks together mean in C#?

前端 未结 18 1125
借酒劲吻你
借酒劲吻你 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:08

    It's short hand for the ternary operator.

    FormsAuth = (formsAuth != null) ? formsAuth : new FormsAuthenticationWrapper();
    

    Or for those who don't do ternary:

    if (formsAuth != null)
    {
      FormsAuth = formsAuth;
    }
    else
    {
      FormsAuth = new FormsAuthenticationWrapper();
    }
    
    0 讨论(0)
  • 2020-11-22 04:10

    coalescing operator

    it's equivalent to

    FormsAuth = formsAUth == null ? new FormsAuthenticationWrapper() : formsAuth
    
    0 讨论(0)
  • 2020-11-22 04:10

    For your amusement only (knowing you are all C# guys ;-).

    I think it originated in Smalltalk, where it has been around for many years. It is defined there as:

    in Object:

    ? anArgument
        ^ self
    

    in UndefinedObject (aka nil's class):

    ? anArgument
        ^ anArgument
    

    There are both evaluating (?) and non-evaluating versions (??) of this.
    It is often found in getter-methods for lazy-initialized private (instance) variables, which are left nil until really needed.

    0 讨论(0)
  • 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<T>, which constrains the generic type arguments that may be substituted for T to value types. Instances of types constructed from System.Nullable<T> can be assigned a value of null; indeed, their values are null by default. Thus, types constructed from System.Nullable<T> may be referred to as nullable value types. System.Nullable<T> 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<int> 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<T>. That syntax allows one to abbreviate:

    System.Nullable<int> 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<T>.Value property is an invalid operation if the type constructed from System.Nullable<T> 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<T>.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.

    0 讨论(0)
  • 2020-11-22 04:10

    The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

    int? variable1 = null;
    int variable2  = variable1 ?? 100;
    

    Set variable2 to the value of variable1, if variable1 is NOT null; otherwise, if variable1 == null, set variable2 to 100.

    0 讨论(0)
  • 2020-11-22 04:11

    If you're familiar with Ruby, its ||= seems akin to C#'s ?? to me. Here's some Ruby:

    irb(main):001:0> str1 = nil
    => nil
    irb(main):002:0> str1 ||= "new value"
    => "new value"
    irb(main):003:0> str2 = "old value"
    => "old value"
    irb(main):004:0> str2 ||= "another new value"
    => "old value"
    irb(main):005:0> str1
    => "new value"
    irb(main):006:0> str2
    => "old value"
    

    And in C#:

    string str1 = null;
    str1 = str1 ?? "new value";
    string str2 = "old value";
    str2 = str2 ?? "another new value";
    
    0 讨论(0)
提交回复
热议问题