The variables cannot be assigned a null value in C#, so to overcome this, a special feature is provided by C#, which assigns a null value to a variable called nullable type, and it does not work with reference type because a null value is already present, it only works with nullable type which is an instance of System.Nullable
Syntax:
Nullable<data_type> variable_name = null;
The above syntax represents the nullable data type in C#. The keyword nullable represents the nullable type which is an instance of System.Nullable
There is also a short cut to this syntax which involves? operator along with data type as mentioned below:
data_type? variable_name = null;
The above syntax represents the nullable data type in C#. The ? mark symbol represents the nullable type. Data type represents the variable’s data type, where variable_name represents the name of the variable, and a null value is assigned to it.
The value of null is lesser than any value; hence comparison operators cannot be used with null, so we use nullable static class. It is considered as a helper class for Nullable types. The nullable static class provides GetUnderlyingType method. The type argument of the nullable types is returned by this method.
Primitive data types are value types, for example, numbers. Value types are stored in the stack and initialized to their default values implicitly by the .NET framework, even if they are not explicitly initialized at the time they are defined. For example, an integer value is initialized to zero by default; a Boolean value is initialized to false by default and so on. Likewise, all of the value types represent default values. None of them can represent null values that are of great prominence in database applications and that representing null is important in such applications. Any value chosen to represent the null value may not fall in the range of values permitted for the value’s data type. For example, if we choose -1 to represent null for a value type, -1 may not be the permitted value for that data type. One should also make sure if a certain value is chosen to represent the null value in an application, that value must not be used elsewhere for any other purposes across the applications. To overcome this issue, the nullable type was provided by C# 2.0. The structure of the System. Nullable is as follows, which can be used to define nullable types:
Code:
namespace System { public struct Nullable : System.IComparable, System.INullableValue { public Nullable(T value); public static explicit operator T(T? value); public static implicit operator T?(T value); public T Value { get; } public bool HasValue { get; } public T GetValueOrDefault(); } }
Here T stands for value type, and the structure accepts one parameter. Any value can be defined as a nullable type by using the syntax.
Syntax:
System.Nullable<data_type> variable_name = null;
Following are the example are given below for C# Nullable.
C# program to illustrate nullable types when no value is assigned to the variables
Code:
using System; public class Geeks { //Defining Main Method public static void Main(string[] args) { // Nullable type is defined Nullable<int> a = null; // We use the method GetValueOrDefault(), the default value is 0 Console.WriteLine(a.GetValueOrDefault()); // Nullable type is defined int? b = null; //We use the method GetValueOrDefault(), the default value is 0 Console.WriteLine(b.GetValueOrDefault()); // non-nullable is defined using nullable type syntax int? a1 = 200; // We use the method GetValueOrDefault(), the default value is 0 but the value that is assigned to the variable is returned Console.WriteLine(a1.GetValueOrDefault()); // non-nullable is defined using nullable type syntax Nullable<int> b1 = 10; // We use the method GetValueOrDefault(), the default value is 0 but the value that is assigned to the variable is returned Console.WriteLine(b1.GetValueOrDefault()); } }
The output of the above code is shown in the snapshot below:
Output:
C# program using nullable type to illustrate the use of nullable.HasValue method.
Code:
using System; public class GFG { //Defining Main Method public static void Main() { // defining the variable m as a nullable type Nullable<int> m = 100; // the value of the variable m is checked Console.WriteLine(m.HasValue); // defining the variable n as a nullable type and assigned a value to it Nullable<int> n = null; // check the value of object Console.WriteLine(n.HasValue); } }
The output of the above code is shown in the snapshot below:
Output:
In this tutorial, we understand the concept of nullable type in C# through definition and then understand the working of nullable type in C#. Then we understand different C# programs using nullable type and their working with their output snapshots included with the programs’ results.
Das obige ist der detaillierte Inhalt vonC#-Nullable. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!