C# Best Practices - Define Fields Appropriately
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的更多相关文章
- C# Best Practices - Define Proper Classes
Application Architecture Define the components appropriately for the application and create project ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- YASM User Manual
This document is the user manual for the Yasm assembler. It is intended as both an introduction and ...
- C#集合--Dictionary
字典(dictionary)是一个集合,其中每个元素都是一个键/值对.字典(Dictionaries)是常用于查找和排序的列表. .NET Framework通过IDictionary接口和IDict ...
- 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 ...
- 我的VSTO之路(五):Outlook初步开发之联系人扩展
原文:我的VSTO之路(五):Outlook初步开发之联系人扩展 上一讲我们完成对Word的介绍,文本开始,我将着重介绍Outlook.Outlook是微软Office中一个非常实用的工具,尤其在一个 ...
- Qt中将QTableView中的数据导出为Excel文件
如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...
- scrapy入门
安装完scrapy后,创建一个新的工程: scrapy startproject tutorial 会创建一个tutorial文件夹有以下的文件: tutorial/ scrapy.cfg tutor ...
- 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 ...
随机推荐
- VC6神迹外挂的DIY
2014年09月05日 ⁄ 综合 ⁄ 共 8724字 ⁄ 字号 小 中 大 ⁄ 评论关闭 (一)外挂一般都能在游戏的界面中按一个热键(比如F12,HOME等),就可以呼出外挂的窗口,然后在里面进行外挂 ...
- Django一对多,多对多操作
简要说明 Django里面的数据库操作O2O&M2M,一般归属于models管理 使用场景 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了).//两个表的 ...
- 百度ueditor富文本编辑器的使用
百度ueditor富文本编辑器的使用 //以下为我在官网下载的ueditor v1.3.5 php版的大楷配置步骤第一步: //配置文件的引入应该比功能文件先引入,最后设置语言类型.即:editor. ...
- 面试常用算法——Longest Palindromic Substring(最长回文子串)
第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...
- jquery.validate 一些技巧
1.Validator.element() Validates a single element, returns true if it is valid, false otherwise. http ...
- android小知识之邮箱地址输入自动完成
虽然不难,但是容易忘记,做个备忘吧 package com.guet.zhuge; import android.app.Activity; import android.os.Bundle; imp ...
- FTL(Flash translation layer)闪存转换层
前面说过,闪存的读写单位为页,而页的大小一般为4KB或8KB,但我们的操作系统读写数据是按HDD的扇区尺寸进行的(512Byte(字节)),更麻烦的是闪存擦除以块作单位,而且未擦除就无法写入,这导致操 ...
- 直接调用类成员函数地址(用汇编取类成员函数的地址,各VS版本还有所不同)
在C++中,成员函数的指针是个比较特殊的东西.对普通的函数指针来说,可以视为一个地址,在需要的时候可以任意转换并直接调用.但对成员函数来说,常规类型转换是通不过编译的,调用的时候也必须采用特殊的语法. ...
- php环境配置优化
Php相关配置 – 基础 max_execution_time = 30 max_input_time = 60 memory_limit = 128 Mmax_input_vars = 1000 r ...
- tlplayer for ios V1.0
此程序UI修改于虎跃在线课堂.所以极其相似. 可以播放网络视频与本地视频,不知道怎么拷贝本地视频到Ipad或iphone上看的朋友,请自己到网上看教程. 支持mms,file,rtsp,rtmp,ht ...