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. yii2.0 从控制器到视图的输出

    在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...

  2. 对discuz的代码分析学习(一)目录结构

    主目录 DISCUZ用的是自己的框架,和其他框架应用一样属于单入口应用.主目录下的.php文件,大部分是应用的入口文件. home.php:家园入口,即论坛中类似博客的那个东西.index.php:首 ...

  3. ObjectiveC 文件操作二

    10,文件委托,以便操作文件.头部看起来像是这样. @interface MyFileManager : NSObject @property(strong)NSFileManager *fileMa ...

  4. sql2012管理

    一.还原完整备份的语法如下: RESTORE DATABASE { database_name | @database_name_var }                  --数据库名 [ FRO ...

  5. HDU 5054 Alice and Bob

    #include <cstdio> int main(){ int n,m,x,y; while(~scanf("%d%d%d%d",&n,&m,&am ...

  6. Drupal7模块multiselect使用

    Drupal二次开发的时候,我们时常要使用到多选列表,但是官方默认的多选下拉列表,是在不敢恭维如下图所示: 不过难看不可怕,Drupal有两万第三方模块做支撑,只有你想不到,没有找不到的. 功夫不负有 ...

  7. S3C6410 GPIO操作接口

    在后面的驱动学习中,需要对GPIO进行一系列的操作,了解这些引脚操作有助于编码的效率. 一.配置GPIO S3C6410要使用其引脚时,需要对其进行配置,如配置为输入/输出/中断等功能,根据芯片手册来 ...

  8. 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. eclipse修改编译路径

    右击项目--properties--java build path--点击source,修改最下方的路径即可

  10. SERVLET API 中 forward() 与 redirect()的区别?

    答:前者仅是容器中控制权的转向, 在客户端浏览器地址栏中不会显示出转向后的地址: 后者则是完全的跳转, 浏览器将会得到跳转的地址, 并重新发送请求链接. 这样, 从浏览器的地址栏中可以看到跳转后的链接 ...