空访问器重要吗?关于值类型及其修改
由于“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
值类型的主要特征是它们是复制的而不是通过引用传递的。
当您有一个值类型和一个访问器时,您本质上有一个从方法返回的值类型,这会导致复制(以下两个示例是相同的):
如果您现在分配给返回的值,则您正在分配x 的副本。因此,对该属性返回的值所做的任何更改都将立即丢失。
当您删除 { get; 时} 访问器,你现在有了一个基本字段,例如:
or
这意味着没有复制,这意味着当分配给该字段时,你不再分配给副本。
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):
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.:
or
Which means no copy is made, which means when assigning to the field, you're no longer assigning to a copy.
p._a.X = 5;
不起作用的原因是p._a
返回a
类型的值。价值观不能改变。但如果将值放入变量中,则可能会更改变量的值。The reason
p._a.X = 5;
won't work is becausep._a
returns a value of the typea
. Values cannot change. But if you put the value in a variable, you may change the value of the variable.您无法删除这两个访问器。
这样:
它可以工作,但它不再是一个属性。
编辑:对于属性,从 p._a 获得的值是函数调用的结果。即使你修改了它,修改后的值也绝不会“写回”到“原始”
_a
。相反,您只需修改 getter 函数返回的临时值。C# 可以允许这样做,但这会导致混乱,因为人们会期望在
p._a.X = 5; 之后int xx = p._a.X;
xx
的值将为 5。但事实并非如此。因为 p_.a 确实不是一个变量:-)不同的是,
你的
_a
是一个字段;如果_a
是一个属性。而且这种情况是不允许的。
You can't delete both accessors.
This way:
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 ofxx
would be 5. But it won't be so. Because p_.a is indeed not a variable :-)The difference is that with
your
_a
is a field; in case of_a
is a property. And the caseis not allowed.