csharp语言的想法 [英] csharp language idea

查看:72
本文介绍了csharp语言的想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在尝试更改属性返回的结构时,

编译器会生成错误。


所以如果你尝试


someControl.Location.Y = 10;


如果能理解值类型的工作原理,你会得到一个合乎逻辑的错误。

但是允许这样的构造并且让

编译器在这种情况下生成这样的代码并不是一个好主意:


Point p = someControl.Location;

pY = 10;

someControl.Location = p;


如果有一组在物业的访问者,这肯定会

可能。在大多数情况下,这样的属性将是微不足道的,因此它们将被内联并且临时变量的开销可以被优化掉。

你怎么看待它? />

currently when trying to change a struct returned by a property, the
compiler generates an error.

so if you try

someControl.Location.Y = 10;

you get an error which is logical if one understands how value types work.
but wouldn''t it be an good idea to allow such a construct and let the
compiler generate code like that in such a case:

Point p = someControl.Location;
p.Y=10;
someControl.Location = p;

In cases where there is a set accessor in the property this will certainly
possible. In most cases such properties will be trivial so they will get
inlined and the overhead with the temporary variable can be optimized away.
What do you think about that?

推荐答案

这与结构无关。它与属性有关。

Control的Location属性是一个Point。因为Point是一个值

类型,当你访问该属性时,你可以通过值访问它,也就是说,你得到原始Point的副本而不是
对原始

点的引用。你当然可以单独更改任何结构的成员。但是

不是这种情况。


你*可以*做的是创建一个新点并将其分配给位置

属性,或者你可以设置Left属性,它对应于

Location.X属性,或者你可以设置Top属性来改变

Location.Y 。


-

HTH,


Kevin Spencer

Microsoft MVP

专业Numbskull


努力工作是一种药物,

没有安慰剂。


cody <德******** @ gmx.de>写在消息中

新闻:eW ************** @ TK2MSFTNGP05.phx.gbl ...
This really has nothing to do with structs. It has to do with properties.
The Location property of a Control is a Point. Because a Point is a value
type, when you access the property, you access it by value, that is, you get
back a copy of the original Point rather than a reference to the original
Point. You can certainly change the members of any struct individually. But
not in this case.

What you *can* do is to create a new Point and assign it to the Location
property, or you can set the Left property, which corresponds to the
Location.X property, or you can set the Top property to change the
Location.Y.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"cody" <de********@gmx.de> wrote in message
news:eW**************@TK2MSFTNGP05.phx.gbl...
目前正在尝试更改由属性返回的结构,
编译器会生成错误。

所以如果你尝试

someControl.Location.Y = 10;
<如果你理解了值类型是如何工作的,那么你会得到一个合乎逻辑的错误。
但是允许这样的结构并让
编译器生成这样的代码并不是一个好主意。这样的情况:

点p = someControl.Location;
pY = 10;
someControl.Location = p;

如果有的话在物业中设置一个存取器,这肯定是可能的。在大多数情况下,这样的属性将是微不足道的,因此它们将被内联并且临时变量的开销可以被优化

您如何看待它?
currently when trying to change a struct returned by a property, the
compiler generates an error.

so if you try

someControl.Location.Y = 10;

you get an error which is logical if one understands how value types work.
but wouldn''t it be an good idea to allow such a construct and let the
compiler generate code like that in such a case:

Point p = someControl.Location;
p.Y=10;
someControl.Location = p;

In cases where there is a set accessor in the property this will certainly
possible. In most cases such properties will be trivial so they will get
inlined and the overhead with the temporary variable can be optimized
away.
What do you think about that?



是的,它会起作用。我会把它留给Jon Skeet这样的名人来说

说出编译器是否能够在每种情况下弄清楚要做什么。




但是,我认为它会掩盖许多新手C#程序员心中已经混浊的东西:
:何时使用课程和

什么时候使用结构。


就我而言,我觉得框架设计者使得Point

和Rectangle可变结构令人讨厌。我认为让它们保持不变会更清楚

并要求你这样说:


点newPoint = new Point(oldPoint.X, 10);


将Y坐标更改为10,例如。是的,它更详细,

但它消除了尝试做你指出的诱惑和产生编译器错误的
的诱惑。


现在看来,Point和Rectangle可以改变......

有时......取决于特定值的行为

情况。像这样的东西只是刺入我的克拉。


所以,是的,两个解决方案将是:1)使结构不可变,或

2)尝试修复编译器拒绝设置

结构属性的情况。

Yes, it would work. I''ll leave it to luminaries such as Jon Skeet to
say whether the compiler would be capable of figuring out what to do in
every situation.

However, I think that it would obscure something that is already cloudy
in the minds of many newbie C# programmers: when to use classes and
when to use structs.

For my part, I find it annoying that the Framework designers made Point
and Rectangle mutable structs. I think that it would have been clearer
to have left them immutable and require you to say this:

Point newPoint = new Point(oldPoint.X, 10);

to change the Y coordinate to 10, for example. Yes, it''s more verbose,
but it removes the temptation to try to do what you pointed out and
which generates a compiler error.

As it stands right now, Point and Rectangle can be altered...
sometimes... depending upon the behaviour of values in the particular
situation. Stuff like that just sticks in my craw.

So, yes, the two solutions would be: 1) make the struct immutable, or
2) try to fix the situations in which the compiler refuses to set
struct properties.


Kevin Spencer< ke *** @ DIESPAMMERSDIEtakempis.com>写道:
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
这实际上与结构无关。它与属性有关。


我不同意。它有*所有*与结构有关。

Control的Location属性是一个Point。因为Point是一个值
类型,当您访问该属性时,您可以通过值访问它,也就是说,您将获得原始Point的副本而不是对原始点。
This really has nothing to do with structs. It has to do with properties.
I disagree. It has *everything* to do with structs.
The Location property of a Control is a Point. Because a Point is a value
type, when you access the property, you access it by value, that is, you get
back a copy of the original Point rather than a reference to the original
Point.




此时它被分类为值,而不是变量。确切地说 -

这与结构有什么关系?


来自C#ECMA规范的第14.3.1节(C#v1.1) ):


< quote>

当在struct-type中声明的属性或索引器是

的目标时赋值,与属性关联的实例表达式或

索引器访问必须归类为变量。如果实例

表达式被归类为值,则会发生编译时错误。

< / quote>


这正是'在这里发生的' - 但是这里的实例表达式

被归类为一个值,因此就是错误。


那个限制确实如此不适用于课程,因此它有一切可以用结构,IMO来做



-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget 博客: http://www.msmvps.com /jon.skeet

如果回复小组,请不要给我发邮件



At which point it''s classified as a value, not a variable. Exactly - in
what way does this have nothing to do with structs?

From section 14.3.1 of the C# ECMA spec (v1.1 of C#):

<quote>
When a property or indexer declared in a struct-type is the target of
an assignment, the instance expression associated with the property or
indexer access must be classified as a variable. If the instance
expression is classified as a value, a compile-time error occurs.
</quote>

That''s exactly what''s happening here - but the instance expression here
is classified as a value, hence the error.

That restriction does not apply to classes, hence it has everything to
do with structs, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于csharp语言的想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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