C# Struct method doesn't save value if accessed by a property

后端 未结 4 857
挽巷
挽巷 2020-12-06 14:58

I need to create a structure that looks like an int (but has an extra field that I need...), so I created a new structure named TestStruct added one method (test()) that I n

4条回答
  •  甜味超标
    2020-12-06 15:18

    When you use Val, the property is returning a copy , it's that copy you modify, then you access the property again which gets another copy.

    If you change the struct to a class this problem should go away because if the difference in how classes and structs are treated.

提交回复
热议问题