1024programmer Asp.Net Revisiting value types and reference types in C#

Revisiting value types and reference types in C#

In C#, there are two types of data types: value type and reference type. A reference type variable stores a reference to data, and the data is stored in the data heap, while a value type variable stores data directly. With reference types, two variables can refer to the same object. Therefore, operations on one variable may affect objects referenced by another variable. With value types, each variable has its own copy of the data, and operations on one variable cannot affect another variable.

Value Type

All value types inherit from the ValueType class, which better adapts to value types by overloading the virtual methods of Object.

Although ValueType is the implicit base class of value types, you cannot directly create classes that inherit from ValueType.

There are two types of value:

  • Structure Type (Structure Type), used to encapsulate data and related functions, including custom Structs and built-in structs (such as integers, floats, booleans, chars, and value tuples)
  • Enum Type, defined by a set of named constants, representing an option or combination of options.

The structural type directly inherits from System.ValueType, while the enumeration type inherits from System. Enum.

The value type can be empty, use System.Nullable (or T?) generic type, such as: int?, bool?. Therefore, System.Nullable itself is also a value type.

public struct Nullable where T :   struct

You can use the Struct constraint to specify that a type parameter is a non-nullable value type (struct types and Enum types all satisfy the constraints).

Value types cannot be inherited, because all value types will eventually be compiled into final classes (sealed), but structures can implement interfaces .

Here is a custom structure:

public struct Location
 {
     public double X;
     public double Y;
     public Location(double   x, double y) => (X, Y) = (x, y);
 }

Instantiate the value objects a and b with the same data respectively, and make equal judgments. The output results are as follows:

var a = new Location(1, 2  );
 var b = new   Location(1,   2);
 Console.WriteLine(a.Equals(b)); // true
 b.Y = 3;
 Console.WriteLine(a.Equals(b)); // false

It can be seen from this: the value type equality comparison is the data itself.

Reference Type

The most common reference type is class (class), also includes string, array, delegate, interface, record, etc. All reference types inherit from Object.

String (string): A special reference type that cannot be inherited and is immutable, but it is more like a value type in usage.

string a = "123";
 string b = a;
 a = "456  ";
 Console.WriteLine(a); // "456"
 Console.WriteLine(b); // "123"

Record (record): Introduced in C# 9.0, it is not a new syntax, but syntactic sugar. Used to define a reference type that provides built-in encapsulated data functionality.

public record Person(string   FirstName, string LastName);

Here is a custom class:

public class Location
 {
     public double X;
     public double Y;
     public Location(double   x, double y) => (X, Y) = (x, y);
 }

The reference objects a and b with the same data are also instantiated separately, and the equality judgment is performed. The output results are as follows:

var a = new Location(1,2  );
 var b = new   Location(1,  2);
 var c = b  ;
 Console.WriteLine(a == b); // false
 Console.WriteLine(a.Equals(b)); // false
 Console.WriteLine(b == c); // true

It can be seen from this that the reference type equality compares the reference address, not the data itself.

Comparison of value types and reference types

1. The value type is allocated on the stack or inline in the structure, and the reference type is allocated on the heap.

2. Value type variable assignment copies the object itself, while reference type variable assignment copies the object reference.

3. Value types and reference types ultimately inherit Object.

4. Both structure and reference types in value types can implement interfaces.

5. Value types cannot be inherited, because all value types are sealed, and reference types can derive new types (except string).

6. The value type has better efficiency in memory management and is suitable for use as a carrier for storing data. Reference types support polymorphism and are suitable for defining the behavior of an application.

Reference:

Value types – C# reference | Microsoft Learn

Reference types – C# Reference | Microsoft Learn

Author: Tian Xingjian Gentleman to Self-improvement
Source: https://www.cnblogs.com/fengjq/p/17589245.html (please specify when reprinting)
If this article is helpful to you, please click [Recommend] in the lower right corner, welcome to leave a message in the comment area. This article has been synchronized to the author’s WeChat public account: Play DotNet, thank you for your attention!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/revisiting-value-types-and-reference-types-in-c/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索