Resolve nullable types

Time:2022-1-15

C# single question mark? And double question mark??
? The single question mark is used to assign null to data types that cannot be directly assigned null, such as int, double, bool, etc., which means that this data type is nullable.

int? i = 3;
Equivalent to:

Nullable<int> i = new Nullable<int>(3);
int i; // Default 0
int? ii; // The default value is null
?? Double question marks are used to determine that a variable returns a specified value when it is null.

Next, let’s explain in detail.

C # nullable type
C # provides a special data type, nullable type (nullable type). Nullable type can represent the value within the normal range of its basic value type, plus a null value.

For example, nullable < int32 >, read as “nullable int32”, can be assigned any value between – 2147483648 and 2147483647, or null. Similarly, the nullable < bool > variable can be assigned true or false or null.

The ability to assign null to numeric or Boolean types is particularly useful when dealing with databases and other data types that contain elements that may not be assigned. For example, a Boolean field in a database can store the value true or false, or the field can be undefined.

The syntax for declaring a nullable type (nullable type) is as follows:

< data_type> ? <variable_name> = null;
The following example demonstrates the use of nullable data types:

example
using System;
namespace CalculatorApplication
{
class NullablesAtShow
{

static void Main(string[] args)
  {
     int? num1 = null;
     int? num2 = 45;
     double? num3 = new double?();
     double? num4 = 3.14157;
     
     bool? boolval = new bool?();

     //Display value
     
     Console. Writeline ("display nullable type values: {0}, {1}, {2}, {3}",
                        num1, num2, num3, num4);
     Console. A writeable value ("line boo}");
     Console.ReadLine();

  }

}
}
When the above code is compiled and executed, it will produce the following results:

Display nullable type values:, 45,, 3.14157
An nullable Boolean value:
Null merge operator (?)
The null merge operator is used to define default values for nullable and reference types. The null merge operator defines a default value for type conversion to prevent nulls for nullable types. The null merge operator implicitly converts the operand type to the type of an operand of another nullable (or non nullable) value type.

If the value of the first operand is null, the operator returns the value of the second operand; otherwise, it returns the value of the first operand. The following example demonstrates this:

example
using System;
namespace CalculatorApplication
{
class NullablesAtShow
{

static void Main(string[] args)
  {
     
     double? num1 = null;
     double? num2 = 3.14157;
     double num3;
     num3 = num1 ??  5.34;      //  Num1 returns 5.34 if NULL
     Console. Writeline ("value of Num3: {0}", Num3);
     num3 = num2 ?? 5.34;
     Console. Writeline ("value of Num3: {0}", Num3);
     Console.ReadLine();

  }

}
}
When the above code is compiled and executed, it will produce the following results:

Num3 value: 5.34
Num3 value: 3.14157