C# Best Practices - Creating Good Properties
Coding Properties
Code in the Getter
Check 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
Features
Concise 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 values
public 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 control
Execute 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.
C# Best Practices - Creating Good Properties的更多相关文章
- C# Best Practices - Creating Good Methods
How to Define a Method Identify the problem => Define the single purpose => Specify the inputs ...
- Table Properties [AX 2012]
Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Ap ...
- Base Enum Properties [AX 2012]
Base Enum Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: December ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- DbContext运行时动态附加上一个dbset
参考 Creating DbSet Properties Dynamically C# code? 1 DbSet<MyEntity> set = context.Set<MyEnt ...
- Running Solr with Maven
Solr is an open source search server which is built by using the indexing and search capabilities of ...
- My ECMAScript 7 wishlist
With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...
- 基于xml文件实现系统属性配置管理
文章标题:基于xml文件实现系统属性配置管理 . 文章地址: http://blog.csdn.net/5iasp/article/details/11774501 作者: javaboy2012 E ...
- SAP HANA学习资料大全[非常完善的学习资料汇总]
Check out this SDN blog if you plan to write HANA Certification exam http://scn.sap.com/community/ha ...
随机推荐
- python基础学习笔记6--异常
异常 1.什么是异常? python用异常对象来表示( exception object)异常情况.如果异常没有被处理或扑捉,程序就会所谓的回溯(TraceBack,一种错误信息)而终止执行: 2. ...
- php基础知识总结
PHP 代表 PHP: Hypertext Preprocessor PHP 文件可包含文本.HTML.JavaScript代码和 PHP 代码 PHP 代码在服务器上执行,结果以纯 HTML 形式返 ...
- WM_SYSCOMMAND包括很多功能,比如:拖动左边框、拖动标题栏、滚动条滚动、点击最小化、双击标题栏——Delphi 通过事件代替了大部分常用的消息,所以Delphi 简单、易用、高效
procedure TForm1.WMSysCommand(var Message: TWMSysCommand); var str: string; begin case Message.CmdTy ...
- css 自适应布局阮一峰
转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...
- sublime的20个插件
SublimeText是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁,功能非同凡响,性能快得出奇.这些非常棒的特性 包括任意跳转(Goto Anything).多重选择( ...
- HDU 5274 Dylans loves tree(树链剖分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...
- Uber 司机有话说:你以为当个 Uber 司机很轻松?大错特错!
Uber 最近的负面新闻越来越多.各方成员都在抨击.斥责.揭露 Uber 公司的各种黑幕.今天,来自 Uber 公司的司机为您讲述咱「拼车老司机」自己的故事.你以为开着自己的私家车出去满城市的晃悠接客 ...
- JWPlayer 使用小记
最后的效果 1.从官网下载JWPlayer 下载后的文件,标红部分是必要的文件. 2.Jquery可以使用1.6以上版本 <html><head> <title>G ...
- How draw a stem -and -leaf & box-plot display by R.or Python
参考: 使用R进行数据可视化套路之-茎叶图.盒形图 step by step R 读取数据 在网上下载的2013全国各省区GDP排名(exl文件) 先 另存为 data.txt(为了方便存到D盘文件夹 ...
- ASP.NET Web编程
runat="server"直接回交服务器,处理数据,又以数据加密后的hidden属性的input控件插入回去,实现表单的状态保存 ruant="server" ...