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的更多相关文章

  1. C# Best Practices - Creating Good Methods

    How to Define a Method Identify the problem => Define the single purpose => Specify the inputs ...

  2. Table Properties [AX 2012]

    Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Ap ...

  3. Base Enum Properties [AX 2012]

    Base Enum Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: December ...

  4. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  5. DbContext运行时动态附加上一个dbset

    参考 Creating DbSet Properties Dynamically C# code? 1 DbSet<MyEntity> set = context.Set<MyEnt ...

  6. Running Solr with Maven

    Solr is an open source search server which is built by using the indexing and search capabilities of ...

  7. My ECMAScript 7 wishlist

    With ECMAScript 6 now feature complete, any further changes to the core of JavaScript will happen in ...

  8. 基于xml文件实现系统属性配置管理

    文章标题:基于xml文件实现系统属性配置管理 . 文章地址: http://blog.csdn.net/5iasp/article/details/11774501 作者: javaboy2012 E ...

  9. SAP HANA学习资料大全[非常完善的学习资料汇总]

    Check out this SDN blog if you plan to write HANA Certification exam http://scn.sap.com/community/ha ...

随机推荐

  1. Canvas 雾玻璃

    Demo http://lumixraku.github.io/demos/Fog/Fog.html Canvas tutorial 给大家安利一个学习Canvas的站点 http://www.htm ...

  2. Umbraco学习2------数据类型

    一.基础概念 在使用Umbraco这类CMS制作网站之前,先要搞清楚的是,和概念中网站制作的区别. 暂时忘掉所谓的ADO.NET存储.忘掉ASP.NET.忘掉多层架构什么的. 只需要关注:要显示什么. ...

  3. Windows XP密钥(共38枚)

    翱翔博客(http://hi.baidu.com/guoguo6688/home) Windows XP Professional VOL版密钥:=========================== ...

  4. 用extundelete恢复rm -rf删的文件

    “慎用rm -rf命令,除非你知道此命令带来的后果.”这是一条Linux用户守则,虽然大多数用户都明白这条语句的含义,但是我觉得还需要完善一下,为这条语句加 上一个使用前提:在你确认自己拥有清醒头脑, ...

  5. Windows Azure 新上线网络相关服务

     动态路由网关.点到站点(Point to Site)VPN正式商用 动态路由网关和点到站点VPN支持基于路由的VPN,并且允许用户将独立计算机连接到Azure上的虚拟网络.现在,虚拟网络中的动态 ...

  6. android 数据持久化——I/O操作

    上一节中简单的介绍了File的操作,这一节来说说使用android平台自带对象实现文件的基本操作 主要的两个类:openFileOutput(写)和openFileInput(读) 向文件中写如数据代 ...

  7. javascrip cookie

    首先要明白一下cookie的概念.由于HTTP协议是一种无状态协议,也就是说一旦server和client的数据交换完成后,他们之间的连接就会被断开.再次交换数据的时候就须要再次建立连接.这就意味着s ...

  8. Android应用开发基础篇(4)-----TabHost(选项卡)

    一.概述 TabHost是一种用来显示标签的组件,不清楚?看一下来电通这个应用就知道了.这个组件用起来与其他组件不太一样,它需要继承TabActivity这个类,还有它的布局文件与我们平时用的也有些不 ...

  9. Filter 字符编码Filter 一

    使用字符编码Filter package com.helloweenvsfei.filter; import java.io.IOException; import javax.servlet.Fil ...

  10. UVa340 Master-Mind Hints

    #include <stdio.h>#include <string.h> #define MIN(a,b) (((a) < (b)) ? (a) : (b)) int ...