C#属性:对于getter和setter而言,没有更多的语法糖 [英] C# properties: nothing more syntactic sugar for getters and setters

查看:137
本文介绍了C#属性:对于getter和setter而言,没有更多的语法糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


在我看来,C#属性只不过是

getter和setter的语法糖。我想知道其他人是否持有不同的观点。

基本上,他们还提供什么?


感谢您的回复,

最好的问候,


Neil

Hello,

It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil

推荐答案

Neil Zanella< nz******@cs.mun.ca>写道:
Neil Zanella <nz******@cs.mun.ca> wrote:
在我看来,C#属性只不过是
getter和setter的语法糖。我想知道其他人是否持有不同的观点。
基本上,他们还提供什么呢?
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Basically, what more do they have to offer?




没什么 - 就像使用声明一样(和指令,来吧)

只提供语法糖。当然,所有这些都给了

令人难以置信的*有用的*语法糖...


实际上,有*属性和只有*之间的区别

有一个吸气剂和一个二传手 - CLR知道它也是一个属性,所以

它被反映为财产。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

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



Nothing - just as the using statement (and directive, come to that)
offers nothing more than syntactic sugar. Of course, all of these give
incredibly *useful* syntactic sugar...

Actually, there *is* a difference between having a property and just
having a getter and a setter - the CLR knows it''s a property too, so
it''s exposed in reflection as a property.

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


" Neil Zanella" < NZ ****** @ cs.mun.ca>在消息中写道

news:b6 ************************** @ posting.google.c om ...
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...
你好,

在我看来,C#属性只不过是getter和setter的语法糖
。我想知道其他人是否持有
视图的不同点。基本上,他们还提供什么?

感谢您的回复,

最诚挚的问候,

Neil
Hello,

It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have to offer?

Thank you for your replies,

Best Regards,

Neil




什么废话。



What bullshit.


Jon Skeet [C#MVP]写道:
Jon Skeet [C# MVP] wrote:
Neil Zanella< nz * *****@cs.mun.ca>写道:
Neil Zanella <nz******@cs.mun.ca> wrote:
在我看来,C#属性只不过是吸气剂和制定者的语法糖。我想知道其他人是否持有不同的观点。


句法suger运行多种语言:for(s1; e; s2)s3;"是句法的

糖为s1;而(e){s3; S2; }",但非常有用。


使用(T t = e){s1; ...; sN; }"是(几乎,因为它也可以使用

Close())语法糖为T t = t;试试{s1; ...; sN;终于{

t.Dispose(); }"

基本上,它们还提供什么?
It seems to me that C# properties are nothing more than syntactic sugar for
getters and setters. I wonder whether others hold a different point of view.
Syntactic suger runs many languages: "for(s1; e; s2) s3;" is syntactic
sugar for "s1; while(e) { s3; s2; }", but very usefull.

The "using(T t = e) { s1;...;sN; }" is (almost, since it can also uses
Close()) syntactic sugar for "T t = t; try { s1;...;sN; } finally {
t.Dispose(); }"
Basically, what more do they have to offer?




它们提供了一种声明字段的方法;在接口中,(如.Count)


它们提供了一种现在只声明公共字段的方法,你可以

总是添加一些代码来验证稍后更新该字段

使用该字段更改代码的语法。


此外,它们上的类型系统有点不同于获取/设置,因为

a属性是一个实体和获取/设置是两个函数,具有单独的

类型。


在声明接口时这是重要的:


接口Foo {int i {get; } $

声明一个接口,其中Foo的任何实现都可以为i设置一个set

部分。虽然这不是一个真正有用的限制声明,但它是一个相当恼人的限制,适用于一些类:)


A(可能有点混乱)限制的解决方法是:


class Bar:Foo

{

int Foo.i {get {return i; } $

public int i {get {...} set {...}}

}


哪个让我们人们用bar.i = 5来设置我如果他们知道酒吧是一个实例

吧,但不是如果他们知道它是Foo。


-

Helge



They provide a way to declare "fields" in interfaces, (like .Count)

They provide a way to just declare a public field right now, you can
always add some code to verify the updates of the field later without
changing the syntax of the code using the field.

Also, the type-system on them is a bit different than on get/set, since
a property is "one" entity and get/set are two functions with separate
types.

This is importent when declaring interfaces:

interface Foo { int i { get; } }

Declares an interface where NO implementation of Foo can have a "set"
part for i. While this is not really a usefull restriction declare, it
is a rather annoying restriction to have applied to ones classes :)

A (slightly confusing maybe) workaround for the restriction is:

class Bar: Foo
{
int Foo.i { get { return i; } }
public int i { get { ... } set { ...} }
}

Which lets people set i with "bar.i = 5" if they know bar as an instance
of Bar, but not if they know it as Foo.

--
Helge


这篇关于C#属性:对于getter和setter而言,没有更多的语法糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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