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 ...
随机推荐
- Use API to retrieve data from internet
Reference: Working with APIs Many big companies and organizations provide API for us to retrieve dat ...
- vb ——ini 配置文件
最近在学校VB 开发点小东西, 使用ini配置文件要用到下边连个函数 GetPrivateProfileString (从配置文件得到信息)百度百科的介绍http://baike.baidu.com/ ...
- 随机数、continue、break
arc4random() — 返回一个随机数(无符号整型). 如果要随机一个 [a, b]范围内的整数 公式:arc4random() % (b - a + 1) + a; #include &l ...
- APM代码学习笔记3:执行过程
以Linux平台ArduPlane为例 \ArduPlane\Plane.cpp 定义Plane类 继承自AP_HAL::HAL::Callbacks ,获取hal对象. \ArduPlane\Ard ...
- 工具类_java 数字转化为汉字大写
public class Num2Rmb { private String[] hanArr = { "零", "壹", "贰", &quo ...
- java MemCachedClient遍历memcache中所有的key
在java memcached client documentation中没有提共遍历memcache所有key的方法.但是提供了两个方法statsItems和statsCacheDump,通过sta ...
- 第七届河南省赛10403: D.山区修路(dp)
10403: D.山区修路 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 69 Solved: 23 [Submit][Status][Web Bo ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- Java Native Interface Specification(JNI)
Java Native Interface Specification(JNI) 使用场景: 需要的功能,标准的java不能提供 有了一个用其他的语言写好的工具包,希望用java去访问它 当需要高性能 ...
- 关于js封装框架类库之DOM操作模块(一)
在前端开发的过程中,javascript极为重要的一个功能就是对DOM对象的操作,而对其封装就是为了更好地进行DOM操作,提高浏览器的支持效率 现在给出一个案例:页面创建三个div,然后给其添加样式 ...