如果由一个属性访问C#结构体的方法不保存价值 [英] C# Struct method doesn't save value if accessed by a property

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

问题描述

我需要建立一个结构,看起来像一个int(但有一个额外的领域,我需要...),所以我创建了一个名为TestStruct新结构增加了一个方法(测试()),我需要和重载一些运营商,它似乎运作良好...

下面的示例显示了问题。如果结构试验()方法是从瓦尔财产执行,那么瓦尔属性似乎失去了价值,但如果该方法是在VAL2变量执行,这一次似乎保持正确的价值...

为什么会出现这种情况?

 静态类节目
{
    ///<总结>
    ///的主入口点的应用程序。
    ///< /总结>
    [STAThread]
    静态无效的主要()
    {
        新的TestClass();
    }
}

公共类识别TestClass
{
    公共TestStruct的Val {获得;组; }
    私人TestStruct VAL2;

    公众识别TestClass()
    {
        Val.test();
        Console.WriteLine(VAL + - >它为什么不是10?);

        //直接分配效果很好...
        瓦尔= 123;
        Console.WriteLine(VAL + - >直接assingment工程..);

        //这样的工作了。它为什么不能一起工作获得;和set;?
        Val2.test();
        Console.WriteLine(VAL2 + - >其以这种方式工作);
    }
}

公共结构TestStruct
{
    私人的Int32 _Value;
    众长偏移{获得;组; }

    公共静态隐含的运营商TestStruct(的Int32值)
    {
        返回新TestStruct {_Value =值};
    }

    公共静态隐含运营商的Int32(TestStruct值)
    {
        返回value._Value;
    }

    公共无效测试()
    {
        _value = 10;
    }
}
 

解决方案

结构是错误的。

对于一个非常大的一些原因,你应该永远使一个可变的结构。

就像一个 INT 的DateTime 价值是不变的,永远无法改变,所以也是一个特定值的结构必须永远不会改变的。

相反,你可以返回一个新的,不同的值的功能。

下面是一些原因,可变的结构是邪恶的:

  1. http://ericlippert.com/2008/05/14/mutating -readonly-结构/
  2. HTTP://blog.slaks。净/ 2010/12 /时,不应该,你写-REF-this.html
  3. HTTP://$c$cblog.jonskeet .UK / 2010/07/27 /迭代,该死的,你/
  4. <一个href="http://philosopherdeveloper.word$p$pss.com/2011/03/31/how-i-discovered-a-bug-in-the-c-compiler-part-1/" rel="nofollow">http://philosopherdeveloper.word$p$pss.com/2011/03/31/how-i-discovered-a-bug-in-the-c-compiler-part-1/

要回答这个问题, Val.test()等同于 get_Val()。测试()
由于结构是值类型,`get_Val()(自动生成的属性getter)返回结构的复制
在私人支持字段原始结构不受影响。

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...

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;
    }
}

解决方案

Your struct is wrong.

For a very large number of reasons, you should never make a mutable 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/


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#结构体的方法不保存价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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