简化的属性语法? [英] Simplified property syntax?

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

问题描述

是否存在简化的属性语法?


例如我们必须写:

class MyClass {

private int _field;

public int Field {get {return _field;} set {_field = value}}

}


如果相反你可以写的:


class MyClass {

public property int Field;

}


有这样的语法吗?如果不是的话?看来这个

更清洁,并且可以防止强制为每个

财产单独写一个支持商店。


这样做的好处对于仅使用支持

字段的属性进行更清晰的编码,以及后来更改该字段的实现的兼容性

如果需要而不破坏界面。

解决方案

>是否存在或应该有简化的属性语法?


例如我们必须写:
class MyClass {
private int _field;
public int Field {get {return _field;} set {_field = value}}
}
如果相反你可以写:

class MyClass {
public property int字段;
}
这种语法是否可用?如果不是的话?它似乎更清洁,并且可以防止强制为每个属性单独写一个支持。

这样做的好处是可以更清晰地编写只使用
支持字段,以及后来根据需要更改该字段的实现的兼容性,而不会破坏界面。




我肯定我是遗漏了一些明显的东西,但不是唯一的区别

之间:


public int Field;


和假设:


公共财产int Field;


是反射会将一个标记为字段而另一个标记为属性吗? br />
实际上是否有任何你可以改变的东西,因此改变

界面?


-

Lasse V?gs?ther Karlsen
http:// usinglvkblog。 blogspot.com/

mailto:la *** @ vkarlsen.no

PGP KeyID:0x2A42A1C2




" Lasse V ?gs?ther Karlsen" < LA *** @ vkarlsen.no>在消息中写道

新闻:a0 ************************* @ news.microsoft.co m ... < blockquote class =post_quotes>

是否存在简化的属性语法?

例如我们必须写:
class MyClass {
private int _field;
public int Field {get {return _field;} set {_field = value}}
}
如果相反你可以写下来:
class MyClass {
public property int Field;
}
这种语法是否可用?如果不是的话?它似乎更清洁,并且可以防止强制为每个属性单独写一个支持。

这样做的好处是可以更清晰地编写只使用
支持字段,以及后来根据需要更改该字段的实现的兼容性,而不会破坏界面。



我确定我在这里遗漏了一些明显的东西但并不是唯一的区别:

public int Field;

和假设:

公共财产int Field ;

是反射会将一个标记为一个字段而另一个标记为一个属性吗?
实际上是否有任何可以改变的东西,因此
更改界面 ;?




属性是作为方法实现的(set_Field和get_Field在这个

的情况下)。编译器需要创建这些和底层字段。


Personaly我可以忍受额外打字。





您建议的功能与您的变量公开之间的区别究竟是什么?


" WXS" < WX*@discussions.microsoft.com>在留言中写道

news:68 ********************************** @ microsof t.com ...

是否存在或应该有简化的属性语法?

例如我们必须写:
class MyClass {
private int _field;
public int Field {get {return _field;} set {_field = value}}


如果你可以写的话怎么办:

类MyClass {
公共属性int Field;
}

这种语法是否可用?如果不是的话?似乎
这个更清洁,并且可以防止强制为每个
属性单独编写一个后备存储。

这样做的好处是可以更清晰地编写仅使用的属性一个
支持
字段,以及后续更改该
字段的实现的兼容性,如果需要,不会破坏界面。



Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a backing
field, plus compatibility to later change the implementation for that field
if needed without breaking the interface.

解决方案

> Is there or should there be a simplified property syntax?


For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.



I''m sure I''m missing something obvious here but wouldn''t the only difference
between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus "change
the interface"?

--
Lasse V?gs?ther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2



"Lasse V?gs?ther Karlsen" <la***@vkarlsen.no> wrote in message
news:a0*************************@news.microsoft.co m...

Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}
What if instead you could write:

class MyClass {
public property int Field;
}
Is something like this syntax available? If not should it be? It
seems this is much cleaner and prevents forcing writing a backing
store for each property seperately.

The benefit of this is cleaner coding for properties that just use a
backing field, plus compatibility to later change the implementation
for that field if needed without breaking the interface.



I''m sure I''m missing something obvious here but wouldn''t the only
difference between:

public int Field;

and the hypothetical:

public property int Field;

be that Reflection would tag one as a field and the other as a property?
Would there actually be anything you could change with this and thus
"change the interface"?



Properties are implemented as methods (set_Field and get_Field in this
case). The compiler would need to create these and the underlying field.

Personaly I can live with the extra typing.


Hi,

And what exactly is the difference between your proposed feature and making
your variable public?

"WXS" <WX*@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...

Is there or should there be a simplified property syntax?

For example we have to write:
class MyClass {
private int _field;
public int Field { get { return _field;} set { _field=value}}
}

What if instead you could write:

class MyClass {
public property int Field;
}

Is something like this syntax available? If not should it be? It seems
this
is much cleaner and prevents forcing writing a backing store for each
property seperately.

The benefit of this is cleaner coding for properties that just use a
backing
field, plus compatibility to later change the implementation for that
field
if needed without breaking the interface.



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

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