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. 以正方教务系统为例,用php模拟登陆抓取课表、空教室

    课程格子和超级课程表这两个应用,想必大学生都很熟悉,使用自己的学号和教务系统的密码,就可以将自己的课表导入,随时随地都可以在手机上查看. 其实稍微了解一点php的话,我们也可以做一个类似这样的web ...

  2. 【转】Centos 设置IP地址的几种方式

    对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 查看I ...

  3. JSWING小工具

    项目中需要一个发送指令到指定服务端,取得设备ID及检测数据的小工具,就利用jswing开发了一个简单小组件,最终效果如下: 代码很简单,具体片段如下: Window.java import java. ...

  4. Java和Android开发IDE---IntelliJ IDEA使用技巧(转)

    以前一直使用的是Eclipse,听别人介绍说IDEA非常不错,也为了以后转Android studio铺垫下.就开始尝试用idea来开发. 这篇文章主要学习了idea的使用技巧. IDEA 全称 In ...

  5. JSP打印九九乘法表

    ##index.jsp: <%@ page language="java" import="java.util.*" pageEncoding=" ...

  6. one Infos

    Backend Server is still running (PID:). Please try 'euc-server stop' first. (原因很简单,进程控制脚本无restart函数功 ...

  7. Mac下搭建quick cocos2d-x编译环境

    一. 我知道在你的电脑中一定已经安装好了Xcode(没有自己下载去吧),打开Xcode,开启"偏好设置"对话框(commond + ,).假设打开之后出现的是这种一个对话框,那么直 ...

  8. python 内部函数,以及lambda,filter,map等内置函数

    #!/usr/bin/python #encoding=utf-8 def back(): return 1,2, "xxx" #python 可变参数 def test(*par ...

  9. SMACSS:一个关于CSS的最佳实践-3.Layout Rules

    本篇笔者要介绍的是Layout Rules.看完本篇,大家将会知道Layout Rules的作用,以及哪些CSS应该归类为Layout Rules. 什么是Layout Rules? Layout R ...

  10. JavaWeb解释一下什么是 servlet?

    Servlet是一种独立于平台和协议的服务端的java技术,可以生成动态WEB页面与传统的CGI(计算机图形接口)和其他类似的CGI技术相比.Servlet具有更好的可移植性.更强大的功能,更少的投资 ...