C# Best Practices - Creating Good Properties
2016-01-12 18:18
330 查看
Coding Properties
Code in the GetterCheck the user's credentials
Check application state
Format the returned value
Log
Lazy Loading related data/object
Code in the Setter
Check the user's credentials
Check application state
Validate the incoming value
Log or change tracking
Format, convert, clean up
Best Practices
Do:
Add code in the getter to protect, format, initialize,...
Add code in the setter to protect, format, validate,...
Avoid:
Single character name
Abbreviations
Easy Way to Create
prop
propfull
Auto-Implemented Properties
FeaturesConcise property declaration
Implicit backing field
Don't allow code in the getter or setter
Best used for simple properties
Best Practices
Do:
Initialize on the declaration when needed
Avoid:
If property requires code in getter or setter
Property Accessibility
public string Category { get; set; }protected string Category { get; set; }
internal string Category { get; set; }
protected internal string Category { get; set; }
private string Category { get; set; }
in getter or setter
public string Category { get; private set; }
internal string Category { get; private set; }
General rule:
Select the most restricted accessibility that still gets the job done.
Additional Use
1.Define concatenated valuespublic string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } }
2.Express calculations
public int Quantity { get; set; } public int Price { get; set; } public int LineItemTotal { get { return Quantity * Price; } }
3.Expose related object properties
public Vendor ProductVendor { get; set; } public string VendorName { get { return ProductVendor?.CompanyName; } }
Expression-bodied Properties
For C# 6
public string FullName => FirstName + " " + LastName; public int LineItemTotal => Quantity * Price; public string VendorName => ProductVendor?.CompanyName;
Benefits of Properties
Fine grained access controlExecute code
Set break points or logging
Available for data binding
FAQ
1.What is the primary purpose of a property?To guard access to the fields of the class
And optionally provide a location for logic when setter or getter
2.What are auto-implemented properties?
Short cut syntax for defining an implicit backing field with its associated property getter or setter.
3.When should you use an auto-implemented property?
When creating simple properties for a class.
4.When shouldn't you use an auto-implemented property?
If the property requires any code in the getter or setter.
相关文章推荐
- 为Nginx配置图片防盗链
- Tomcat虚拟主机配置 一个Ip对应多个项目
- Linux系统中掩耳盗铃的sudo配置
- xmpp with openfire插件-利用Broadcast实现群
- Linux下echo命令的用法
- Linux删除文件名中包含“-”的文件
- Apache MINA学习笔记
- nginx 缓存控制
- SSL握手通信详解及linux下c/c++ SSL Socket代码举例
- linux的文件类型和权限
- 在virtualbox里把Ubuntu下安装的中文输入法切换出来
- tomcat 内存调整
- linux常用命令——文件管理(二)
- CentOS7.0重置Root的密码
- openssh 加固
- vim设置
- 双三次插值算法的OpenGL实现
- 如何读取argv的数字参数?
- Eclipse+多国语言包插件+Tomcat插件+Lomboz插件+Myeclipse插件的安装与配置
- linux下检查内存泄露的工具--mtrace