Types

Value Types and Reference Types

Value Types / Primitive Types

  • A variable of a value type directly contains their data.

With value types, each variable has its own copy of the data, and it's not possible for operations on one variable to affect the other.

bool, byte, sbyte, char, decimal, double, float, int, uint, nint, nuint, long, ulong, short, ushort, struct, enum, tuple

(double Sum, int Count) t2 = (4.5, 3);    // tuple
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.

Reference Types / Non-Primitive Types

  • A variable of a reference type contains a reference to their data (objects).

With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

class, interface, delegate, record, object, string, dynamic

string a = "hello";
string b = "h";
// Append to contents of 'b'
b += "ello";
Console.WriteLine(a == b);
Console.WriteLine(object.ReferenceEquals(a, b));

Last updated