Home >Backend Development >C++ >What Does the Question Mark Mean in C# Nullable Types (int?)?
C# nullable type: the meaning of question mark
In C#, the question mark (?) is often used in conditional statements. However, it has another meaning when applied to integer equivalent types.
For example, consider the following code snippet:
<code class="language-csharp">public int? myProperty { get; set; }</code>
What does the question mark mean here?
Nullable type
The key to understanding the question mark here is the concept of nullable types. Nullable types are instances of the System.Nullable
structure in C#. They allow us to represent a wider range of values for a given type, including the possibility of null values.
For example, in the above code, int? myProperty
means that myProperty
is a nullable integer. This means it can hold any value in the range of integers, and can also be set to null.
Advantages and examples of nullable types
Nullable types offer many advantages, especially when working with databases or external data sources:
The following is an example illustrating the use of nullable types:
<code class="language-csharp">class NullableExample { static void Main() { int? num = null; // 表示缺失值 // 检查 null if (num.HasValue) { Console.WriteLine("num = " + num.Value); } else { Console.WriteLine("num = Null"); } } }</code>
The above is the detailed content of What Does the Question Mark Mean in C# Nullable Types (int?)?. For more information, please follow other related articles on the PHP Chinese website!