- Visual Studio 2008
- Visual Studio 2005
- Visual Studio 2013
- Visual Studio 2012
The ??? operator is called a null merge operator, which defines the default values for types and reference types that can be null values. If the left operand of this operator is not NULL, this operator returns the left operand, otherwise the right operand is returned.
Notes
A nullable type can contain a value, or it can be undefined.the.? operator defines the default value that is returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a type that does not have a nullable value , then a compile-time error is generated. a InvalidOperationException exception is thrown if a cast is used and a type that can be a null value is not currently defined .
For more information, see nullable Types (C # Programming Guide).
Even?? the two arguments of an operator are constants, and they cannot be treated as constants.
Example C # Replication
Class nullcoalesce{StaticInt? Getnullableint () {ReturnNull }StaticString GetStringValue () {ReturnNull }Staticvoid 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 was null, in which case Assign //Default value O f 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 the which case Display "Unspecified". Console.WriteLine (s?? "Unspecified"); }}
?? Operator (C # Reference)