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. Ubuntu14下LAMP环境的安装以及yaf扩展的安装

    前段时间在ubuntu下安装了lamp环境,记录一下安装过程方便以后查阅. 安装lamp环境 ① 安装apache sudo apt-get install apache2 系统会弹出如图所示的提示, ...

  2. Ubuntu 12.04 LTS下logomaker的安装

    学校嵌入式课程实验,本地装的时候遇到了一系列问题,因为基本不会linux所以到处搜解决方法,中间还走了不少弯路,作个笔记. 1.解压安装倒是没什么问题,运行时提示找不到共享库 logomaker: e ...

  3. Arduino Micro USB库

    USBCore.cpp #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_co ...

  4. GC(Garbagecollection)垃圾回收

    在前面向大家讲解FTL时,我们提到了GC的操作,所谓GC就是把一个闪存块里的‘有效’页数据复制到一个‘空白’块里,然后把这个块完全擦除.GC是SSD里的一个非常关键的操作,其效率对性能有决定性影响.闪 ...

  5. 解决Flex4 发布后访问 初始化极其缓慢的问题

    原文http://blog.163.com/vituk93@126/blog/static/170958034201282222046364/ 昨天找了个免费.net空间,想测试一下做的一个简单Fle ...

  6. C++的类型萃取技术

    应该说,迭代器就是一种智能指针,因此,它也就拥有了一般指针的所有特点——能够对其进行*和->操作.但是在遍历容器的时候,不可避免的要对遍历的容器内部有所了解,所以,设计一个迭代器也就自然而然的变 ...

  7. 一、Cocos2dx在visualStudio或者vc++中环境搭建(入门篇)

    本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=106 0.概述 Cocos2dx-win32的项目能够被向导生成 向导支持vs2008,vs2010 ...

  8. DICOM医学图像处理:DCMTK在VS2012中的配置

    背景: 近期因为项目须要,将原本的开发IDE环境由VS2008升级到了VS2012.本以为编译完毕后的DCMTK开源库能够直接从VS2008移植到VS2012.可是通过项目属性加入完包括文件夹和依赖库 ...

  9. xml 充当简易数据库

    后台: 写入节点 public static void Update(string path, string node, string attribute, string value) { try { ...

  10. 用JavaScript判断横屏竖屏问题

    判断手机横竖屏状态: //判断手机横竖屏状态: function hengshuping() { if(window.orientation == 180 || window.orientation= ...