Home >Backend Development >C++ >What Does the Question Mark Mean in C# Value Types (e.g., `int?`)?
. In this case, what is the role of this question mark? public int? myProperty
The question mark behind the value type indicates that it is a empty type. Can empty type is a special example of the structure, which can accommodate the entire effective range of the underlying type, and a special vacancy.
This function is particularly useful when processing database scenarios or other elements that may intentionally lack value. For example, a database list that allows Boolean values may contain True, FALSE, or NULL. System.Nullable
Consider the following code fragment:
Here, Variables are defined as an air -available integer (), which can save an integer or NULL value.
The attribute allows us to check whether the variable contains effective values. If<code class="language-csharp">class NullableExample { static void Main() { int? num = null; // 判断 HasValue 是否为 true if (num.HasValue) { System.Console.WriteLine("num = " + num.Value); } else { System.Console.WriteLine("num = Null"); } // 如果 num 为 null,则赋值默认值 0 int y = num.GetValueOrDefault(); // 如果 num.HasValue 为 false,则抛出 InvalidOperationException try { y = num.Value; } catch (System.InvalidOperationException e) { System.Console.WriteLine(e.Message); } } }</code>to get the actual value. Otherwise, is considered NULL.
The above is the detailed content of What Does the Question Mark Mean in C# Value Types (e.g., `int?`)?. For more information, please follow other related articles on the PHP Chinese website!