Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

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

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...