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. python之单例设计模式

    设计模式之单例模式 单例设计模式是怎么来的?在面向对象的程序设计中,当业务并发量非常大时,那么就会出现重复创建相同的对象,每创建一个对象就会开辟一块内存空间,而这些对象其实是一模一样的,那么有没有办法 ...

  2. 走进C标准库(6)——"string.h"中函数的实现memchr

    我写的memchr: void *memchr(const void *buf, char ch, unsigned count){ unsigned ; while(*(buf++) != ch & ...

  3. 一些特殊css

    属性 描述            outline  (轮廓)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. outline:#00FF00 dotted thick; 可以按顺序 ...

  4. SQL Server 内存不足引起的并发症

    第一:cpu 1.内存不足就会有频繁的页面调入调出.这个过程是要有cpu的参与的.所以这个要影响cpu! 2.内存不足可能会引有用起执行计划被清除.当起次要执行时.这个就要重编译一次!

  5. uglifyjs note

    uglifyjs UglifyJS is a JavaScript parser, minifier, compressor or beautifier toolkit. uglifyjs是用js写的 ...

  6. ProFTPD 初探

    ProFTPD:一个Unix平台上或是类Unix平台上(如Linux, FreeBSD等)的FTP服务器程序.

  7. /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found,解压rpm包

    如果是64位系统报错信息如下: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by 原因是没有GLIB ...

  8. 深入浅出Win32多线程程序设计之基本概念

    一.深入浅出Win32多线程程序设计之基本概念[转] 引言 从单进程单线程到多进程多线程是操作系统发展的一种必然趋势,当年的DOS系统属于单任务操作系统,最优秀的程序员也只能通过驻留内存的方式实现所谓 ...

  9. Java中byte转int的方法

    byte转化为int有两种情况: 1)要保持数值不变 应用场景:数值计算.等等. 方法:能够直接採用强制类型转换:int i = (int) aByte, 比如:若aByte=0xff(即数值为-1) ...

  10. C标准中一些预定义的宏

    C标准中指定了一些预定义的宏,对于编程经常会用到.下面这个表中就是一些常常用到的预定义宏. 宏(双下滑线) 意义 __DATE__ 进行预处理的日期(“Mmm dd yyyy”形式的字符串文字) __ ...