ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

[Professional C# 7] Use of Properties and Methods

2022-06-24 11:31:48  阅读:249  来源: 互联网

标签:Use set C# class value should Professional property any


➤ Client code should be able to read its value. Write-only properties are not recommended, so, for exam-
ple, use a SetPassword method, not a write-only Password property.
➤ Reading the value should not take too long. The fact that something is a property usually suggests that
reading it will be relatively quick.
➤ Reading the value should not have any observable and unexpected side effect. Furthermore, setting the
value of a property should not have any side effect that is not directly related to the property. Setting
the width of a dialog has the obvious effect of changing the appearance of the dialog on the screen.
That’s fine, because that’s obviously related to the property in question.
➤ It should be possible to set properties in any order. In particular, it is not good practice when setting a
property to throw an exception because another related property has not yet been set. For example, to
use a class that accesses a database, you need to set ConnectionString, UserName, and Password,
and then the author of the class should ensure that the class is implemented such that users can set
them in any order.
➤ Successive reads of a property should give the same result. If the value of a property is likely to change
unpredictably, you should code it as a method instead. Speed, in a class that monitors the motion of
an automobile, is not a good candidate for a property. Use a GetSpeed method here; but Weight and
EngineSize are good candidates for properties because they will not change for a given object.

标签:Use,set,C#,class,value,should,Professional,property,any
来源: https://www.cnblogs.com/FH-cnblogs/p/16408175.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有