To understand Nullable in C#, what types of Nullable in C# are there, readers can refer to the following article by Mytour.
Nullable in C# is what?
Diving into the depths of C#, we ask: What is Nullable?
Understanding Nullable in C# involves grasping the concept of accommodating null values within defined data types.
Delving into C#, how do we declare Nullable variables?
Any data type can be declared nullable by using the '?' operator.
Below is an example of Nullable declaration syntax:
int? i = null;
As mentioned above, 'var' is not compatible with Nullable.
Therefore, Compile Time will return an error message if we declare as follows:
var? i = null;
3. Using Nullable in C#
Nullable is used when passing parameters to Stored Procedures or Databases. If a column in a Table allows Null values, then in this case we use Nullable.
Example: Suppose a Stored Procedure accepts 2 parameters @A and @B, it will return parameter @C.
This output parameter can also be Null. So in this case, we use Nullable variables to retrieve Null as well as permitted values.
Basically, Nullable allows us to declare variables in .NET, which can be used in the Database processing.
4. Types of Nullable
4.1. Nullable Incompatibility with General Data Types
It means we cannot perform operations between Nullable and non-nullable data types.
For example:
In the above example, x is nullable and y is non-nullable. So when the program is executed, it will return an error message as follows:
Error 1 Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
To avoid the above error, we need to declare all variables as Nullable as shown below:
The program will run without any Compile Time or Runtime errors.
4.2. Comparing Nullable with Non-Nullable Values
In the previous section, Mytour mentioned the error that would occur when executing Nullable with non-nullable values, however, we can compare these values with each other.
To illustrate this better, readers can refer to the example below:
In the example above, all code snippets work fine and the values are evaluated correctly.
Additionally, the above code also works well with the '!=' operator.
4.3. Null Coalescing Operator in C#
The Null Coalescing Operator in C# is denoted by '??'. Let's further understand the Null Coalescing Operator in C# with the example below:
In the command: result = x ?? y;
The Null Coalescing Operator is assigned to the value of 'x' to 'result', if 'x' is null then it assigns the value of 'y' to 'result'.
So if the first variable is null, that value will be assigned, otherwise the value of the second variable will be assigned. In the example above, 'result' will hold Null.
Note: Variable 'result' can also be Null, so it must be defined as Nullable.
4.4. Assigning Nullable Variable to Non-Nullable Value
Directly assigning a Nullable Variable to a Non-Nullable Value will result in an error.
Refer to the example below:
When executed, the code snippet above will return the following error:
Error 1 Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
To fix the error, we will use the Null Coalescing Operator:
Now when running the program above, the intention will return the value of x as 4. To replace 0 in the statement int y = x ?? 0;, you can use any integer.
Any integer you use will be the default value of 'y'.
This article by Mytour.vn just introduced you to Nullable in C#. Besides, readers can refer to other articles on Mytour to understand more about loops in C# as well as encapsulation in C#.