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. codeforces 377B Preparing for the Contest 二分+优先队列

    题目链接 给你m个bug, 每个bug都有一个复杂度.n个人, 每个人有两个值, 一个是能力值, 当能力值>=bug的复杂度时才可以修复这个bug, 另一个是雇佣他需要的钱,掏一次钱就可以永久雇 ...

  2. get the execution time of a sql statement.

    declare @d datetimeset @d = GETDATE()select * from dbo.spt_valuesselect [语句执行花费时间(毫秒)]= DATEDIFF(ms, ...

  3. WebWorker SharedWorker ServiceWorker

    WebWorker 是什么? 为 JavaScript 引入线程技术 不必再用 setTimeout().setInterval().XMLHttpRequest 来模拟并行 Worker 利用类似线 ...

  4. 生成输出url时,使用CSS来控制超链接的格式

    在前文<生成输出url>中的第5点,介绍了使用ActionLink生成输出url中指定html标签属性. 例如, 假设Global.asax中的路由定义为: public static v ...

  5. java中三种常见内存溢出错误的处理方法

    更多 10   相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的 ...

  6. 原理图产生网络表后导进PADS之后,网络乱了的问题

    问题描述:在Orcad中生成的网络表(格式.ASC),导进PADS9.2中(PADS9.2中已有一些元器件),结果报Mixing nets,如下图示. 仔细检查原理图中的这些nets,发现有的有错,有 ...

  7. Python GUI编程各种实现的对比

    Python GUI编程各种实现的对比 从 Python 语言的诞生之日起,就有许多优秀的 GUI 工具集整合到 Python 当中,这些优秀的 GUI 工具集,使得 Python 也可以在图形界面编 ...

  8. passwordauthentication yes

    ssh ip disconnected:no supported authentication methods available(server sent:publickey,gssapi-keyex ...

  9. mysql的函数

  10. VS2008下直接安装使用Boost库1.46.1版本号

    Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...