如果通过属性访问 C# Struct 方法不会保存值 [英] C# Struct method doesn't save value if accessed by a property

查看:17
本文介绍了如果通过属性访问 C# Struct 方法不会保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个看起来像 int 的结构(但有一个我需要的额外字段...),所以我创建了一个名为 TestStruct 的新结构,添加了一个我需要的方法 (test()) 并重载了一些运营商,它似乎运行良好...

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 needed and overloaded some operators, and it seemed to be working well...

下面的示例显示了问题.如果结构体 test() 方法是从 Val 属性执行的,那么 Val 属性好像丢了值,但是如果在 Val2 变量上执行这个方法,这个好像是保值了...

The sample below shows the problem. If the structure test() method is executed from the Val property then the Val property seems to lose the value, but if the method is executed on the Val2 variable, this one seems to keep the right value...

为什么会发生这种情况?

Why does this happen?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        new TestClass();
    }
}

public class TestClass
{
    public TestStruct Val { get; set; }
    private TestStruct Val2;

    public TestClass()
    {
        Val.test();
        Console.WriteLine(Val + "-> why is it not 10?");

        //Direct assignment works well...
        Val = 123;
        Console.WriteLine(Val  + "-> direct assingment works..");

        //This way works too. Why doesn't it work with "get;" and "set;"?
        Val2.test();
        Console.WriteLine(Val2 + "-> it works this way");
    }
}

public struct TestStruct
{
    private Int32 _Value;
    public long Offset { get; set; }

    public static implicit operator TestStruct(Int32 value)
    {
        return new TestStruct { _Value = value };
    }

    public static implicit operator Int32(TestStruct value)
    {
        return value._Value;
    }

    public void test()
    {
        _Value = 10;
    }
}

推荐答案

你的 struct 是错误的.

出于多种原因,您应该永远创建可变结构.

For a very large number of reasons, you should never make a mutable struct.

就像 intDateTime 值是不可变的并且永远不会改变一样,你的 struct 的特定值也不能在全部.

Just like an int or DateTime value is immutable and can never change, so too a specific value of your struct must never change at all.

相反,您可以创建返回新的不同值的函数.

Instead, you can make functions that return a new, different value .

这里有一些可变结构是邪恶的原因:

Here are some reasons that mutable structs are evil:

  1. http://ericlippert.com/2008/05/14/mutating-readonly-structs/
  2. http://blog.slaks.net/2010/12/when-shouldnt-you-write-ref-this.html
  3. http://codeblog.jonskeet.uk/2010/07/27/iterate-damn-you/
  4. http://philosopherdeveloper.wordpress.com/2011/03/31/how-i-discovered-a-bug-in-the-c-compiler-part-1/

<小时>

回答这个问题,Val.test() 等价于 get_Val().test().
由于结构是值类型,`get_Val()(自动生成的属性 getter)返回结构的副本.
私有支持字段中的原始结构不受影响.


To answer the question, Val.test() is equivalent to get_Val().test().
Since structs are value types, `get_Val() (the automatically-generated property getter) returns a copy of the struct.
The original struct in the private backing field is not affected.

这篇关于如果通过属性访问 C# Struct 方法不会保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆