属性的C#2.0语法是否已关闭? [英] Is C# 2.0 syntax of Properties closed?

查看:63
本文介绍了属性的C#2.0语法是否已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本周在MSDN上发表C#2.0文章......我发现

属性的语法非常奇怪,例如:


public int MyIntValue {< br $>
得到{

// ...

}

受保护的套装{

// ...

}

}


....是否定义了这种语法并且必须使用?


也许不仅仅是我觉得上面的语法是

不是最佳选择。

任何答案和讨论都会非常有趣。


Marcin

解决方案

你好Marcin,


这只是一个示例,您可以使用不同的访问修饰符来获取和设置
。获取是公开的,设置是私人的,等等。


快乐的编码!

Morten Wennevik [C#MVP]

<

Morten Wennevik写道:

嗨Marcin,

这只是一个例子,你可以有不同的访问修饰符
得到并设定。获取是公开的,设置是私有的,等等。

快乐的编码!




Thanx ...但我不喜欢这个语法。因为它是

自我冲突。

首先我设置了对公共公共的财产访问权限。然后我必须设置例如
设置对受保护的访问权限或私人...


从逻辑角度来看,我认为这样做要好得多:


int MyInt {

public get {

// ...

}

protected set {

// ...

}

}


//甚至


public int MyInt {

get {...}

}


protected int MyInt {

设置{...}

}


Marcin



" Marcin GrzA?bski" <毫克******* @ taxussi.no.com.spam.pl>在消息中写道

news:c4 ********** @ nemesis.news.tpi.pl ...

Morten Wennevik写道:

嗨Marcin,

这只是一个例子,你可以使用不同的访问修饰符来获取和设置。获取是公开的,设置是私有的,等等。

快乐的编码!
Thanx ...但我不喜欢这种语法。因为它是自相矛盾的。
首先,我已经设置了对公共公共的财产访问权限。然后我必须设置,例如设置对受保护的访问权限或者私人...

我认为从逻辑角度看起来更好看:

int MyInt {
public get {<
//
}
受保护的集合{
// ...
}
}
//或甚至

public int MyInt {
get {...}
}

protected int MyInt {
设置{...}
}




坦率地说,虽然我并不特别关心现有的语法,但我并不认为
认为其中任何一个都更好。分离访问者使得它更少

即时清楚它们是相关的并且将访问修饰符放在

上,访问者使得它立即变得不那么清楚该属性是公共的(如

与大多数语言有关,无论如何,元数据中的元素属性都有
无可访问性。

Marcin



I red MSDN article of C# 2.0 this week... and i found
very strange syntax for properties e.g.:

public int MyIntValue {
get {
// ...
}
protected set {
// ...
}
}

.... Is this syntax defined and obligatory?

Maybe it''s not only my feeling that aboved syntax is
not a best choice.
Any answer and discussion will be very interesting.

Marcin

解决方案

Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Morten Wennevik [C# MVP]


Morten Wennevik wrote:

Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!



Thanx... but i don''t like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private"...

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}

Marcin



"Marcin GrzA?bski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:c4**********@nemesis.news.tpi.pl...

Morten Wennevik wrote:

Hi Marcin,

This is just an example that you can have different access modifiers for
get and set. Get is public, set is private, and so on.

Happy coding!
Thanx... but i don''t like this syntax. Because it is
self-conflicting.
First i have set property access to public "public" and then i have to
set e.g. set access to "protected" or "private"...

I think that so much better from logical point-of-view looks:

int MyInt {
public get {
// ...
}
protected set {
// ...
}
}

// or even

public int MyInt {
get {...}
}

protected int MyInt {
set { ... }
}



Frankly, while I don''t particularly care for the existing syntax, I don''t
think either of these are any better. Seperating the accessors makes it less
immedaitly clear that they are related and putting the access modifiers on
the accessor makes it less immediatly clear that the property is public(as
far as most langauges are concerned at any rate, in metadata a property has
no accessibility).

Marcin



这篇关于属性的C#2.0语法是否已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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