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 ...
随机推荐
- 《Pointers On C》读书笔记(第三章 数据)
1.在C语言中,仅有4种基本数据类型:整型.浮点型.指针和聚合类型(如数组和结构等). 整型家族包括字符.短整型.整型和长整型,它们都分为有符号和无符号两种. 标准规定整型值相互之间大小的规则:长整型 ...
- SQL Server 排名函数实现
在SQL Server 中有四大排名函数分别是: 1.row_number() 2.ntile() 3.rank() 4.dense_rank() -------------------------- ...
- (转)《JAVA与模式》之模板方法模式
该文章转自:http://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html 在阎宏博士的<JAVA与模式>一书中开头 ...
- JAVA并发,同步锁性能测试
测试主要从运行时间差来体现,数据量越大,时间差越明显,例子如下: package com.xt.thinks21_2; /** * 同步锁性能测试 * * @author Administrator ...
- TMS X-Cloud Todolist with FNC
Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...
- 注册表与盘符(转victor888文章 )
转自: http://blog.csdn.net/loulou_ff/article/details/3769479 写点东西,把这阶段的研究内容记录下来,同时也给研究相关内容的同志提供参考, ...
- Git安装及基本使用
准备: Git软件,github账号. Git安装: 直接百度搜git下载,windows和mac不同平台的.官网上的下载地址很慢或者根本下不了. 默认配置安装. github: 网址:https:/ ...
- ubuntu openstack
https://wiki.ubuntu.com/ServerTeam/CloudArchive/ sudo add-apt-repository cloud-archive:junoLong Term ...
- ios 中生成二维码和相册中识别二维码
iOS 使用CIDetector扫描相册二维码.原生扫描 原生扫描 iOS7之后,AVFoundation让我们终于可以使用原生扫描进行扫码了(二维码与条码皆可)AVFoundation可以让我们从设 ...
- JavaScript中你可能不知道的九件事
今天凑巧去W3School扫了一遍JavaScript教程,发现从中看到了不少自己曾经没有注意过的细节. 我这些细节列在这里.分享给可能相同不知道的朋友: 1.使用 document.write() ...