空访问器重要吗?关于值类型及其修改

发布于 2024-08-27 13:42:41 字数 560 浏览 10 评论 0原文

由于“a”是键入的值,我的以下代码不起作用。但我认为即使没有访问器它也不起作用,但它确实起作用:

class Program
    {
        a _a  //with accessors it WONT compile
        {
            get; 
            set;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p._a.X = 5; //when both accessors are deleted, compiler does not
                        //complain about _a.X not being as variable
        }
    }
    struct a
    {
       public int X;
    }

它不起作用,因为“a”是结构。但是当我从“_a”实例中删除访问器时,它就起作用了。我不明白为什么。 谢谢

I have following code that does not work due to "a" being a value typed. But I thought it would not work even without accessors, but it did:

class Program
    {
        a _a  //with accessors it WONT compile
        {
            get; 
            set;
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p._a.X = 5; //when both accessors are deleted, compiler does not
                        //complain about _a.X not being as variable
        }
    }
    struct a
    {
       public int X;
    }

It does not work as "a" is struct. But when I delete accessors from "_a" instance, it works. I do not understand why.
Thanks

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

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

发布评论

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

评论(3

悲欢浪云 2024-09-03 13:42:41

值类型的主要特征是它们是复制的而不是通过引用传递的。

当您有一个值类型和一个访问器时,您本质上有一个从方法返回的值类型,这会导致复制(以下两个示例是相同的):

ValueType Property { get { return x; } } // Will make a copy of x
ValueType Method() { return x; }    // Will make a copy of x

如果您现在分配给返回的值,则您正在分配x 的副本。因此,对该属性返回的值所做的任何更改都将立即丢失。

当您删除 { get; 时} 访问器,你现在有了一个基本字段,例如:

int field;

or

ValueType field;

这意味着没有复制,这意味着当分配给该字段时,你不再分配给副本。

The main feature of value types is that they are copied rather than being passed by reference.

When you have a value type, and an accessor, you essentially have a value type being returned from a method, which causes a copy (the following two examples are the same):

ValueType Property { get { return x; } } // Will make a copy of x
ValueType Method() { return x; }    // Will make a copy of x

If you now assign to the returned value, you're assigning to a copy of x. So any changes made to the value returned from the property will be immediately lost.

When you remove the { get; } accessor, you've now got a basic field, e.g.:

int field;

or

ValueType field;

Which means no copy is made, which means when assigning to the field, you're no longer assigning to a copy.

維他命╮ 2024-09-03 13:42:41

p._a.X = 5; 不起作用的原因是 p._a 返回 a 类型的值。价值观不能改变。但如果将值放入变量中,则可能会更改变量的值。

The reason p._a.X = 5; won't work is because p._a returns a value of the type a. Values cannot change. But if you put the value in a variable, you may change the value of the variable.

彡翼 2024-09-03 13:42:41

您无法删除这两个访问器。

这样:

a _a;

它可以工作,但它不再是一个属性。


编辑:对于属性,从 p._a 获得的值是函数调用的结果。即使你修改了它,修改后的值也绝不会“写回”到“原始”_a。相反,您只需修改 getter 函数返回的临时值。

C# 可以允许这样做,但这会导致混乱,因为人们会期望在 p._a.X = 5; 之后int xx = p._a.X;xx 的值将为 5。但事实并非如此。因为 p_.a 确实不是一个变量:-)


不同的是,

a _a;

你的 _a 是一个字段;如果

a _a { get; set; }

_a 是一个属性。而且这种情况

a _a { }

是不允许的。

You can't delete both accessors.

This way:

a _a;

it works, but it's not a property any more.


Edit: With a property, the value you get from p._a is a result of a function call. If you even modify it, the modified value will by no means "written back" to the "original" _a. Instead, you will just modify a temporary, returned by the getter-function.

C# could allow this, but it would lead to confusion, since people would expect that after p._a.X = 5; int xx = p._a.X; the value of xx would be 5. But it won't be so. Because p_.a is indeed not a variable :-)


The difference is that with

a _a;

your _a is a field; in case of

a _a { get; set; }

_a is a property. And the case

a _a { }

is not allowed.

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