C# 可空

WBOY
發布: 2024-09-03 15:16:25
原創
834 人瀏覽過

在C# 中不能為變數分配null 值,因此為了克服這個問題,C# 提供了一個特殊功能,它將null 值分配給稱為可空類型的變量,並且它不適用於引用類型,因為null value 已經存在,它僅適用於可為null 的類型,該類型是System.Nullablestruct 的實例,其中T 表示不可為null 的值類型,例如布林類型、整數類型、浮點類型等。

文法:

Nullable<data_type> variable_name = null;
登入後複製

以上語法表示 C# 中可為 null 的資料型態。關鍵字 nullable 表示可空類型,它是 System.Nullablestruct 的實例,其中 T 表示不可為空值類型,例如布林類型、整數類型、浮點類型等。資料類型表示變數的資料類型,其中variable_name代表變數的名稱,並為其分配一個空值。

這個語法還有一個捷徑,其中涉及?運算符以及資料類型如下所述:

data_type? variable_name = null;
登入後複製

以上語法表示 C# 中可為 null 的資料型態。這 ?標記符號表示可為空白類型。 Data type 表示變數的資料類型,其中variable_name 表示變數的名稱,並為其賦予空值。

C# 中可空型態的特性

  • 可空類型的值無法直接存取。 GetValueOrDefault() 方法用於提取指定的值,如果不賦值為null,則傳回預設值,即零。
  • 可以使用可空型別將空值指派給變量,而無需基於參考型別建立可空型別。
  • 值可以指派給可空型別。
  • Nullable HasValue 和 Nullable 可用來檢查值。如果為物件賦值,則傳回 true;如果為物件賦值 null,則傳回 false。如果沒有為物件指派值,則會引發編譯時錯誤。
  • ==並且!運算符可以與可空類型一起使用。
  • 如果將 null 指派給可空類型,則 GetValueOrDefault(T) 方法會給予指派的值或作為預設值給出的預設值。
  • 空合併運算子 (??) 也可用於為可空類型的值賦值。
  • Nullable 類型不支援巢狀 Nullable 類型。
  • 可空型別不支援 var 型。如果 nullable 與 var 一起使用,編譯器會給出編譯時錯誤。

C# 中可空型態的優點

  • 可空類型用於資料庫應用程式。如果資料庫中的列需要空值,則可以使用可空類型為該列指派空值。
  • 未定義的值可以使用可空型別來表示。
  • 可以使用可空型別而不是參考型別來儲存空值。

可空輔助類

null 的值小於任何值;因此比較運算子不能與 null 一起使用,因此我們使用可為 null 的靜態類別。它被視為可空類型的輔助類別。可空靜態類別提供 GetUnderlyingType 方法。此方法傳回可空類型的類型參數。

C# 中可空類型的工作

原始資料型別是值型,例如數字。值類型儲存在堆疊中,並由 .NET 框架隱式初始化為其預設值,即使它們在定義時未明確初始化也是如此。例如,整數值預設初始化為零;布林值預設初始化為 false 等等。同樣,所有值類型都表示預設值。它們都不能表示在資料庫應用程式中非常重要的空值,並且表示空值在此類應用程式中很重要。選擇表示空值的任何值可能不屬於該值的資料類型允許的值範圍。例如,如果我們選擇 -1 來表示某個值類型的 null,則 -1 可能不是該資料類型允許的值。還應確保如果選擇某個值來表示應用程式中的空值,則該值不得在應用程式中的其他位置用於任何其他目的。為了解決這個問題,C# 2.0 提供了可空型別。系統的結構。 Nullable 如下,可用來定義可空型別:

代碼:

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();
}
}
登入後複製

這裡T代表值型,結構體接受一個參數。任何值都可以使用語法定義為可空類型。

文法:

System.Nullable<data_type> variable_name = null;
登入後複製

C# 可空的範例

以下是 C# Nullable 的範例。

範例#1

C# 程式來說明當沒有為變數賦值時可空型別

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# 可空

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);
}
}
登入後複製

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

Output:

C# 可空

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.

以上是C# 可空的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!