kingBook

导航

C# 值类型

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/builtin-types/value-types

值类型的变量包含类型的实例。 它不同于引用类型的变量,后者包含对类型实例的引用。 默认情况下,在分配中,通过将实参传递给方法并返回方法结果来复制变量值。 对于值类型变量,会复制相应的类型实例。

using System.Collections;
using UnityEngine;

public class TestVector : MonoBehaviour {

    public Vector3 velocity { get; private set; }

    void Start () {

        velocity = Vector3.one;

        velocity.Set(0f,0f,0f);
        Debug.Log($"a:{velocity}"); // output: a:(1.0, 1.0, 1.0)

        velocity = Vector3.zero;
        Debug.Log($"b:{velocity}"); // output: b:(0.0, 0.0, 0.0)



        Vector2 v1;
        Vector2 v2 = new Vector2(2, 2);
        v1 = v2;
        v2.x = 3;
        Debug.Log(v1);//output: (2.0, 2.0)
        Debug.Log(v2);//output: (3.0, 2.0)

    }
}

posted on 2022-03-24 10:42  kingBook  阅读(114)  评论(0编辑  收藏  举报