The usage of the value type parameter of Unity's parameter type

Insert image description here

When talking about value type parameters, we can explain them in detail from two aspects: basic data types and structures. Value type parameters refer to data passed to a function or method as a value, rather than a reference.

Basic data types

Value type parameters of primitive data types:

In C#, basic data types are built-in data types, such as integers, floating point numbers, characters, etc. These data types are value types and they are copied by value when passed to a function. This means that modifications to parameters inside the function do not affect the original values ​​outside the function.

Example:

void ModifyInt(int x)
{
    
    
    x = 20; // 修改函数内的副本
}

int number = 10;
ModifyInt(number);
Console.WriteLine(number); // 输出:10,因为函数内的修改不影响原始值

Structure

The value type parameter of the structure:

A structure is a user-defined value type that can contain multiple fields. When you pass a structure as a parameter to a function, a copy of the entire structure is passed to the function. This means that operations inside the function do not affect the original structure instance.

Example:

struct Point
{
    
    
    public int X;
    public int Y;
}

void ModifyPoint(Point p)
{
    
    
    p.X = 20; // 修改函数内的副本
}

Point point = new Point {
    
     X = 10, Y = 15 };
ModifyPoint(point);
Console.WriteLine(point.X); // 输出:10,因为函数内的修改不影响原始结构体实例

Further additions to the structure

In C#, you only need to use newthe keyword when creating an instance of a structure, and there is no need to use newto assign an existing structure instance. This is different from reference types (such as classes), which use the keyword newto create instances, and you can use =the operator to assign one instance to another instance, so that two variables share the same instance.

However, with structures, using =the operator actually copies the value of the structure rather than sharing the same instance. So in most cases, you only need to use newthe keyword to create a new struct instance, without assigning one struct instance to another.

Here is a simple example that demonstrates how to use newthe keyword to create an instance of a struct:

public struct Point
{
    
    
    public int X;
    public int Y;

    public Point(int x, int y)
    {
    
    
        X = x;
        Y = y;
    }
}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        Point point1 = new Point(10, 20); // 创建并初始化结构体实例
        Point point2 = new Point(30, 40); // 创建并初始化另一个结构体实例

        Console.WriteLine($"point1: X = {
      
      point1.X}, Y = {
      
      point1.Y}"); // 输出:point1: X = 10, Y = 20
        Console.WriteLine($"point2: X = {
      
      point2.X}, Y = {
      
      point2.Y}"); // 输出:point2: X = 30, Y = 40
    }
}

In the above example, we used new Point(10, 20)and new Point(30, 40)to create two different Pointinstances of the structure respectively.

Summarize:

When value type parameters are manipulated inside a function, they only affect the copy passed to the function, not the original value or instance. This applies to basic data types and structures. Regardless of whether an integer, floating point number, or a custom structure is passed, the modification of the value type parameter inside the function will not be passed outside the function. This is different from reference types (such as classes), where reference type parameters are passed by reference, and modifications to the parameters within the function will affect the original object.

Guess you like

Origin blog.csdn.net/weixin_74850661/article/details/132597517