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 ...
随机推荐
- Javascript实现DIV的隐藏和出现
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- python进阶3--文件系统
文件系统 python的标准库中包括大量工具,可以处理文件系统中的文件,构造和解析文件名,也可以检查文件内容. pyhton表文件名表示为简单的字符串,另外还提供了一些工具,用来由os.path中平台 ...
- java中三种常见内存溢出错误的处理方法
更多 10 相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...
- Webserver管理系列:9、创password重设盘
网络时代需要记录password太多.一不留神可能会忘记.是否server的password忘记将是一件非常麻烦的事情. Windows Server 2008 它为我们创造password重设盘功能 ...
- 生产环境提升rman备份速度----启动块跟踪
生产环境提升rman备份速度----启动块跟踪 [环境] AIX(5300-08).oracle10g(10.2.0.1.0-64bit) [目标] 因为生产环境数据量较大,欲加快rman备份的速度 ...
- SqlCacheDependency的使用
最近项目需要几秒就获取一次数据,查数据库服务器压力会很大,因此用缓存技术来缓解服务器压力. 使用SqlCacheDependency采用轮询的方式来获取缓存,SqlDependency查询通知的方式来 ...
- BZOJ 1834: [ZJOI2010]network 网络扩容(最大流+最小费用最大流)
第一问直接跑最大流.然后将所有边再加一次,费用为扩容费用,容量为k,再从一个超级源点连一条容量为k,费用为0的边到原源点,从原汇点连一条同样的边到超级汇点,然 后跑最小费用最大流就OK了. ---- ...
- Lucence.Net+添加关键词+分页+排序
1.使用queryparser完成解析搜索请求 2.基本格式如: QueryParser parser=new QueryParser("字段名称","分析器实例&quo ...
- 在Oracle 11g中用看Oracle的共享内存段---------IPCS
很早之前,在一次讲课了,用了命令ipcs,发现oracle的共享内段好小,如下: oracle@mydb ~]$ ipcs -a ------ Shared Memory Segments ----- ...
- php基础教程笔记2
如果学过c.java,php的语法不学即会,但是不同的是php是一种脚本语言,需要解释器才能运行.php又和js不同,js是客户端语言,而php运行在服务器端,它无法获取客户端浏览器信息.但是php又 ...