Are structs 'pass-by-value'?

前端 未结 9 1357
轮回少年
轮回少年 2020-11-30 23:43

I\'ve recently tried to create a property for a Vector2 field, just to realize that it doesn\'t work as intended.

public Vector2 Position { get;         


        
9条回答
  •  悲&欢浪女
    2020-12-01 00:11

    .NET data types are divided into value and reference types. Value types include int, byte, and structs. Reference types include string and classes.

    structs are appropriate instead of classes when they just contain one or two value types (although even there you can have unintended side effects).

    So structs are indeed passed by value and what you are seeing is expected.

提交回复
热议问题