Backing Fields

private string description;
private int productId;

Features

A variable in a class

Holds data for each object

Object's data is only accessiable to that object

Fields are private.

Accessible outside of the class through property getters and setters

private string description = "coming soon";

Optional accessibility modifier + Data type + Name + Optional initialization

Best Practices

Do:

Define a meaningful name

Using camelCasing

Keep fields private

Use properties to provide access to the fields

Avoid:

Single character name

Abbreviations

Initialize to the field's default value (*)

Nullable Types

private decimal? cost;
private DateTime? availabilityDate;

Allow definition of a value OR null

Specified with a "?" suffix on the type

Distinguishes "not set" from the default value

Best Practices

Do:

Use on simple types to distinguish "not set" and "default value"

Use properties of the type such as HasValue and Value as needed

Avoid:

Using them if not necessary

Constants

public const double Pi = 3.14;
public const int Red = 0xFF0000;

Define in a class

Holds a hard-coded value that does not change

Must be assigned to an expression that can be fully evaluated at compile time

Think of a constant as a "compile-time" constant value

Are static

Optional accessibility modifier + "const" keyword + Data type + Name + Assigned Value

Best Practices

Do:

Define a meaningful name

Use PascalCasing

Use for compile-time values that will never change

Avoid:

Single character name

Abbreviations

All upper case

For fields that could change over time

Read-Only Fields

public readonly string DefaultMeasure = GetDefaultMeasure();
public readonly decimal MinimumPrice;
public Product()
{
MinimumPrice = RetrieveMinimumPrice();
}

A variable in a class

Holds a value that is initialized and then not changed

Must be initialized (In the declaration Or in a constructor)

Think of a read-only field as a "runtime" constant value

Optional accessibility modifier + Optional "static" keyword + "readonly" keyword + Data type + Name + Assigned value

Constant vs. Read-Only

Constant Field:

Compile-time constant

Assigned to an expression evaluated at compile time

Assigned on declaration

Only number,Boolean or string

Always static

Read-Only Field:

Runtime constant

Assigned to any valid expression at runtime

Assigned on declaration or constructor

Any data type

Optionally static

FAQ

1.Explain the data encapsulation principle

An object's data should be accessible only to the object

Backing fields containing in the object data should be marked as private

2.What's backing field?

A variable in a class used to retain each object's data.

3.When should you use a back field?

For every data retained field for an object

4.When should you use a constant?

When defining a field with a simple data type that will never change.

5.When should you use a read-only field?

When defining a field that is initialized from a file,table,or code but should not then be changed anywhere else in the application.

6.What is the difference between a constant and a read-only field?

A constant is static, assigned in the declaration, assigned to an expression that is fully evaluated at compile time.

A read-only field can be static or non-static,assigned in the declaration or constructor, assigned to any valid expression.

C# Best Practices - Define Fields Appropriately的更多相关文章

  1. C# Best Practices - Define Proper Classes

    Application Architecture Define the components appropriately for the application and create project ...

  2. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  3. YASM User Manual

    This document is the user manual for the Yasm assembler. It is intended as both an introduction and ...

  4. C#集合--Dictionary

    字典(dictionary)是一个集合,其中每个元素都是一个键/值对.字典(Dictionaries)是常用于查找和排序的列表. .NET Framework通过IDictionary接口和IDict ...

  5. oracle Form Builer:FIND_FORM Built-in

    Description Searches the list of forms and returns a form module ID when it finds a valid form with ...

  6. 我的VSTO之路(五):Outlook初步开发之联系人扩展

    原文:我的VSTO之路(五):Outlook初步开发之联系人扩展 上一讲我们完成对Word的介绍,文本开始,我将着重介绍Outlook.Outlook是微软Office中一个非常实用的工具,尤其在一个 ...

  7. Qt中将QTableView中的数据导出为Excel文件

    如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...

  8. scrapy入门

    安装完scrapy后,创建一个新的工程: scrapy startproject tutorial 会创建一个tutorial文件夹有以下的文件: tutorial/ scrapy.cfg tutor ...

  9. Java EE (2) -- Java EE 6 Enterprise JavaBeans Developer Certified Expert(1z0-895)

    Introduction to Java EE Gain an understanding of the Java Platform, Enterprise Edition (Java EE) Exa ...

随机推荐

  1. linux下通过脚本实现自动重启程序的方法

    无论什么程序都不可能完美无缺,理论上,任何程序都有 Core Dump 的一天,正式运营的程序,尤其是服务器程序,一旦 Core Dump ,后果不堪设想,有过服务器开发经验的朋友,一定都经历过深夜美 ...

  2. 将四个BYTE数值转换成IEEE754标准的浮点数(两种方法:用Addr函数取字节数字的首地址,或者用Absolute关键字)

    在工作中,经常使用到IEEE754格式的数据.IEEE754格式的数据占四个字节,好像Motorola格式和Intel格式的还不一样. 由于工作中很少和他打交道(使用的软件内部已经处理),就没太在意. ...

  3. rsyslog 不打印日志到/var/log/messages

    *.info;mail.none;authpriv.none;cron.none;local3.none /var/log/messages 表示 所有来源的info级别都记录到/var/log/me ...

  4. kbengine FAQ(3)

    官方FAQ 下面是我遇到的一些问题: 1.服务端   "strtof" 未定义的标示符 对于我这个c++入门级都不算的新手,这个错误很挠头,这个错误是由于vc++版本问题,新版的语 ...

  5. 翻纸牌游戏(dfs回溯)

    翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  6. 转载ajax

    写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...

  7. MS Chart 学习心得

    利用Chart控件对学生信息进行统计,最终结果如下: Chart图形控件主要由以下几个部份组成: 1.Annotations --图形注解集合2.ChartAreas  --图表区域集合3.Legen ...

  8. 【JAVA】导出jar包时,Class files on classpath not found

    是因为\META-INF\MANIFEST.MF文件里面配置错误 错误版本 Manifest-Version: 1.0Class-Path: 正确版本 Manifest-Version: 1.0Cla ...

  9. JavaScript使用button提交表单

    <form action="test.html" method="POST"> <input type="button" ...

  10. C++对象模型--C++对象模型

    何为C++对象模型? 部分: 1       语言中直接支持面向对象程序设计的部分 2       对于各种支持的底层实现机制 语言中直接支持面向对象程序设计的部分,如构造函数.析构函数.虚函数.继承 ...