Backing Fields

  1. private string description;
  2. 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

  1. 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

  1. private decimal? cost;
  2. 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

  1. public const double Pi = 3.14;
  2. 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

  1. public readonly string DefaultMeasure = GetDefaultMeasure();
  2. public readonly decimal MinimumPrice;
  3. public Product()
  4. {
  5. MinimumPrice = RetrieveMinimumPrice();
  6. }

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. VC6神迹外挂的DIY

    2014年09月05日 ⁄ 综合 ⁄ 共 8724字 ⁄ 字号 小 中 大 ⁄ 评论关闭 (一)外挂一般都能在游戏的界面中按一个热键(比如F12,HOME等),就可以呼出外挂的窗口,然后在里面进行外挂 ...

  2. Python数据分析扩展库

    Anaconda和Python(x,y)都自带了下面的这些库. 1. NumPy 强大的ndarray和ufunc函数. import numpy as np xArray = np.ones((3, ...

  3. QR Code簡介、介紹

    QR Code為目前最常被使用的一種二維條碼,1994年由日本Denso-Wav e公司發明,QR是英文Qu ickResponse的縮寫,即快速反應的意思,1999年時,公布了符合日本當地的標準-「 ...

  4. MySQL Binlog的介绍

    binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...

  5. display:table标签来自动改变列宽 改变的同时table的整体宽度跟随变化

    发现公司里的所有分页功能都是通过display:talbe来实现的,但是用户最近说要让表格列宽可以拖动:所有我就寻找了好多的办法:网上找了很多的资料,但是都不是我要的效果因为他们都是列宽不改变要不就是 ...

  6. geometry(简单数学题)

    geometry  Accepts: 324  Submissions: 622  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  7. uva 10404 Bachet's Game(完全背包)

    题目连接:10404 - Bachet's Game 题目大意:由一堆石子, 给出石子的总数, 接下来由stan和ollie两个人玩游戏,给出n, 在给出n种取石子的方法(即为每次可取走石子的数量), ...

  8. Google Maps 学习笔记(三)

    1.GPolyline折线对象和GPolygon多边形对象 html标签中必须包含v="urn:schemas-microsoft--com:vml"的命名空间 <html ...

  9. SQL Server索引进阶:第一级,索引简介

    这个并不是我翻译的,全文共有15篇,但我发现好多网站已经不全,所以自己整理. 原文地址: Stairway to SQL Server Indexes: Level 1, Introduction t ...

  10. Hadoop学习笔记(4)hadoop集群模式安装

    具体的过程参见伪分布模式的安装,集群模式的安装和伪分布模式的安装基本一样,只有细微的差别,写在下面: 修改masers和slavers文件: 在hadoop/conf文件夹中的配置文件中有两个文件ma ...