如果我们不进行任何数据验证,我们应该使用属性吗? [英] Should we use properties if we are not doing any data validation

查看:54
本文介绍了如果我们不进行任何数据验证,我们应该使用属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们没有进行任何数据验证,我们是否应该使用属性

Should we use properties if we are not doing any data validation

推荐答案

是的,你应该。



属性与验证无关。这只是抽象功能,它允许您呈现语法,如设置或从字段读取值,并隐藏此类赋值(=)语法背后的副作用。通常情况下,属性是由某些基础字段支持,这些字段被认为(以良好的编程风格)是私有的,但这不是必需的。然而,对于属性具有一种或另一种赋值语义是好的,否则应该使用一些其他特征。



通常,使用非私有字段被认为是是一种糟糕的编程风格。如果您有一些具有赋值语义的公共成员,那么它应该是一个属性。它允许项目的更好的可支持性:即使分配或读取某个字段的值不需要任何副作用,您可能需要稍后实现它。在这种情况下,您可以使用自动定义的属性。如果以后要添加一些副作用,则保留现有属性成员,仅添加 getter setter 。这样,您就不需要使用属性更改任何代码,与您拥有字段的情况相反,后来决定将其提升为方法或两种方法来分配和读取某些值。



请参阅: http://msdn.microsoft.com/ en-us / library / x9fsa0sw.aspx [ ^ ]。



-SA
Yes, you should.

Properties are not related to validation. This is just the abstraction feature which allows you to present the syntax like in setting or reading a value to/from a field and hide side effects behind such assignment ("=") syntax. Typically, a property is backed by some underlying fields which is supposed (in a good programming style) to be private, but this is not required. However it's good to have one or another kind of assignment semantic for a property, otherwise some other features should be used.

Usually, using non-private fields is considered to be a bad style of programming. If you have some public member with assignment semantic, it should be a property. It allows for better supportability of projects: even if assignment or reading a value of some field does not require any side effect, you may need to implement it later. In this case, you can use auto-defined properties. If later you want to add some side effect, you leave the existing property member, only add a getter or a setter. This way, you won't need to change any code using your property, in contrast to the situation of you had a field and later decide to promote it to a method or two method to assign and read some value.

Please see: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx[^].

—SA


这篇关于如果我们不进行任何数据验证,我们应该使用属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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