结构体中字段/属性的分配

发布于 2024-12-02 08:39:00 字数 720 浏览 0 评论 0原文

可能的重复:
修改字典中的结构变量

为什么它

  MyStruct test = new MyStruct();
  test.Closed = true;

效果很好,但

MyDictionary[key].Closed = true;

显示编译时出现“无法修改表达式,因为它不是变量”错误?

为什么这两种情况下的分配不同?

注意: MyDictionary 的类型为

结构体代码:

public struct MyStruct
{
    //Other variables
    public bool Isclosed;
    public bool Closed
    {
        get { return Isclosed; }
        set { Isclosed = value; }
    }
//Constructors
}

Possible Duplicate:
Modify Struct variable in a Dictionary

Why is it that

  MyStruct test = new MyStruct();
  test.Closed = true;

Works great, but

MyDictionary[key].Closed = true;

Shows a "Cannot modify the expression because it is not a variable" error at compile time?

Why is different about the assignment in these two cases?

Note: MyDictionary is of type <int, MyStruct>

Code for the struct:

public struct MyStruct
{
    //Other variables
    public bool Isclosed;
    public bool Closed
    {
        get { return Isclosed; }
        set { Isclosed = value; }
    }
//Constructors
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2024-12-09 08:39:00

因为 MyDictionary[key] 返回一个结构体,所以它实际上返回集合中对象的副本,而不是使用类时发生的实际对象。这就是编译器警告您的内容。

要解决此问题,您必须在对象更改后重新设置 MyDictionary[key],可能如下所示:

var tempObj = MyDictionary[key];
tempObj.Closed = true;
MyDictionary[key] = tempObj;

Because MyDictionary[key] returns a struct, it is really returning a copy of the object in the collection, not the actual object which is what happens when you use a class. This is what the compiler is warning you about.

To work around this, you'll have to re-set MyDictionary[key] after the changes to the object, perhaps like this:

var tempObj = MyDictionary[key];
tempObj.Closed = true;
MyDictionary[key] = tempObj;
橘虞初梦 2024-12-09 08:39:00

将结构更改为类......

class Program
{
    static void Main(string[] args)
    {
        dic = new Dictionary<int, MyStruct>();

        MyStruct s = new MyStruct(){ Isclosed=false};

        dic.Add(1,s);

        dic[1].Isclosed = true;

        Console.WriteLine(dic[1].Isclosed.ToString()); //will print out true...
        Console.Read();
    }

    static Dictionary<int, MyStruct> dic;

    public class MyStruct
    {
        public bool Isclosed;
        public bool Closed
        {
            get { return Isclosed; }
            set { Isclosed = value; }
        }
    }
}

Change the struct to be a class instead...

class Program
{
    static void Main(string[] args)
    {
        dic = new Dictionary<int, MyStruct>();

        MyStruct s = new MyStruct(){ Isclosed=false};

        dic.Add(1,s);

        dic[1].Isclosed = true;

        Console.WriteLine(dic[1].Isclosed.ToString()); //will print out true...
        Console.Read();
    }

    static Dictionary<int, MyStruct> dic;

    public class MyStruct
    {
        public bool Isclosed;
        public bool Closed
        {
            get { return Isclosed; }
            set { Isclosed = value; }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文