C#-Nullable

WBOY
Freigeben: 2024-09-03 15:16:25
Original
945 Leute haben es durchsucht

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.Nullablestruct where T represents non-nullable value types such as Boolean type, integer type, floating-point type, etc.

Syntax:

Nullable<data_type> variable_name = null;
Nach dem Login kopieren

The above syntax represents the nullable data type in C#. The keyword nullable represents the nullable type which is an instance of System.Nullablestruct where T represents non-nullable value types such as Boolean type, integer type, floating-point type, etc. 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.

There is also a short cut to this syntax which involves? operator along with data type as mentioned below:

data_type? variable_name = null;
Nach dem Login kopieren

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.

Characteristics of Nullable Type in C#

  • The values of the nullable type cannot be accessed directly. GetValueOrDefault() method is used to extract the assigned value if it is not assigned to null, and if it is null, the default value is returned, which is zero.
  • A null value can be assigned to a variable using a nullable type without needing to create a nullable type based on the reference type.
  • Values can be assigned to nullable types.
  • Nullable HasValue and Nullable can be used to check the value. If a value is assigned to an object, true is returned, and false is returned if null is assigned to the object. If there is no value assigned to the object, a compile-time error is raised.
  • == and ! operators can be used with nullable types.
  • If null is assigned to the nullable type, GetValueOrDefault(T) method gives the assigned value or the default value that is given as default.
  • Null-coalescing operator (??) can also be used to assign a value to the value of the nullable type.
  • Nested Nullable types are not supported by Nullable type.
  • A nullable type does not support var type. The compiler gives Compile-time error if nullable is used with var.

Advantages of Nullable Type in C#

  • Nullable type is used in database applications. If a column in a database requires null values, a nullable type can be used to assign null values to the column.
  • Undefined values can be represented using nullable types.
  • A null value can be stored using a nullable type rather than using a reference type.

Nullable Helper Class

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.

Working of Nullable Type in C#

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();
}
}
Nach dem Login kopieren

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;
Nach dem Login kopieren

Examples of C# Nullable

Following are the example are given below for C# Nullable.

Example #1

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());
}
}
Nach dem Login kopieren

The output of the above code is shown in the snapshot below:

Output:

C#-Nullable

Example #2

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);
}
}
Nach dem Login kopieren

The output of the above code is shown in the snapshot below:

Output:

C#-Nullable

Conclusion

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!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage