數值型別 (c # 參考) Value types (C# reference)

數值型別參考型別是 c # 型別的兩個主要類別。Value types and reference types are the two main categories of C# types. 實值型別的變數包含型別的實例。A variable of a value type contains an instance of the type. 這與參考型別的變數不同,後者包含型別實例的參考。This differs from a variable of a reference type, which contains a reference to an instance of the type. 根據預設,在 指派時,將引數傳遞至方法,並傳回方法結果,會複製變數值。By default, on assignment, passing an argument to a method, and returning a method result, variable values are copied. 在實數值型別變數的情況下,會複製對應的型別實例。In the case of value-type variables, the corresponding type instances are copied. 下列範例示範了該行為:The following example demonstrates that behavior:

using System;

public struct MutablePoint
{
    public int X;
    public int Y;

    public MutablePoint(int x, int y) => (X, Y) = (x, y);

    public override string ToString() => $"({X}, {Y})";
}

public class Program
{
    public static void Main()
    {
        var p1 = new MutablePoint(1, 2);
        var p2 = p1;
        p2.Y = 200;
        Console.WriteLine($"{nameof(p1)} after {nameof(p2)} is modified: {p1}");
        Console.WriteLine($"{nameof(p2)}: {p2}");

        MutateAndDisplay(p2);
        Console.WriteLine($"{nameof(p2)} after passing to a method: {p2}");
    }

    private static void MutateAndDisplay(MutablePoint p)
    {
        p.X = 100;
        Console.WriteLine($"Point mutated in a method: {p}");
    }
}
// Expected output:
// p1 after p2 is modified: (1, 2)
// p2: (1, 200)
// Point mutated in a method: (100, 200)
// p2 after passing to a method: (1, 200)

如先前的範例所示,實數值型別變數的作業只會影響儲存在變數中的實值型別實例。As the preceding example shows, operations on a value-type variable affect only that instance of the value type, stored in the variable.

如果實值型別包含參考型別的資料成員,則在複製值型別實例時,只會複製參考型別的實例參考。If a value type contains a data member of a reference type, only the reference to the instance of the reference type is copied when a value-type instance is copied. 複製和原始實數值型別實例都可以存取相同的參考型別實例。Both the copy and original value-type instance have access to the same reference-type instance. 下列範例示範了該行為:The following example demonstrates that behavior:

using System;
using System.Collections.Generic;

public struct TaggedInteger
{
    public int Number;
    private List<string> tags;

    public TaggedInteger(int n)
    {
        Number = n;
        tags = new List<string>();
    }

    public void AddTag(string tag) => tags.Add(tag);

    public override string ToString() => $"{Number} [{string.Join(", ", tags)}]";
}

public class Program
{
    public static void Main()
    {
        var n1 = new TaggedInteger(0);
        n1.AddTag("A");
        Console.WriteLine(n1);  // output: 0 [A]

        var n2 = n1;
        n2.Number = 7;
        n2.AddTag("B");

        Console.WriteLine(n1);  // output: 0 [A, B]
        Console.WriteLine(n2);  // output: 7 [A, B]
    }
}

注意

若要讓您的程式碼較不容易出錯且更健全,請定義和使用不可變的實數值型別。To make your code less error-prone and more robust, define and use immutable value types. 本文僅針對示範目的使用可變的實數值型別。This article uses mutable value types only for demonstration purposes.

實數值型別的種類Kinds of value types

實值型別可以是下列兩種類型之一:A value type can be one of the two following kinds:

  • 封裝資料和相關功能的結構類型a structure type, which encapsulates data and related functionality
  • 列舉型別,它是由一組命名常數所定義,代表一個選擇或一個選擇組合。an enumeration type, which is defined by a set of named constants and represents a choice or a combination of choices

可為 null的實值型別 T? 代表其基礎實值型別的所有值 T ,以及一個額外的null值。A nullable value type T? represents all values of its underlying value type T and an additional null value. 您無法指派 null 給實值型別的變數,除非它是可為 null 的實值型別。You cannot assign null to a variable of a value type, unless it's a nullable value type.

內建實數值型別Built-in value types

C # 提供下列內建實數值型別,也稱為 簡單類型C# provides the following built-in value types, also known as simple types:

所有簡單類型都是結構類型,而且與其他結構類型不同,因為它們允許某些額外的作業:All simple types are structure types and differ from other structure types in that they permit certain additional operations:

  • 您可以使用常值提供簡單類型的值。You can use literals to provide a value of a simple type. 例如,'A'char 型別的常值,而 2001 則是 int 型別的常值。For example, 'A' is a literal of the type char and 2001 is a literal of the type int.

  • 您可以使用 const 關鍵字來宣告簡單型別的常數。You can declare constants of the simple types with the const keyword. 不可能有其他結構類型的常數。It's not possible to have constants of other structure types.

  • 常數運算式(其運算元都是簡單類型的常數)會在編譯時期評估。Constant expressions, whose operands are all constants of the simple types, are evaluated at compile time.

從 c # 7.0 開始,c # 支援 值元組Beginning with C# 7.0, C# supports value tuples. 值元組是實數值型別,但不是簡單類型。A value tuple is a value type, but not a simple type.

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格的下列幾節:For more information, see the following sections of the C# language specification:

另請參閱See also