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 ...
随机推荐
- Linux 网络编程基础(3) -- 数据的IO
首先介绍两个数据结构及相关的操作函数:struct iovec.struct msghdr struct iovec { void * iov_base; /*向量的缓冲地址*/ size_t ...
- Qt保证只有一个实例(将CreateMutex得到的handle通过转换得到值)
使用CreateMutex 可以实现只启动一个应用程序实例 view plaincopy to clipboardprint?#include <QApplication>#include ...
- iPhone 5s网络钓鱼邮件,和苹果发布会同步亮相
正如预期的一样,网络犯罪分子会利用Apple最新发表的iPhone 5s消息,几乎在苹果的新产品发表会同时,这个网络钓鱼(Phishing)信件开始流传.此次,趋势科技病毒防治中心 Trend Lab ...
- 滴滴司机:要不是Uber,我买奥迪的45万元不知何时赚回来呢!
你在专车里看风景,看风景的人在系统上看你.补贴装饰了你的出行,你装饰了平台的梦. 这是移动互联网时代城市人们每天出行的形象写照.受“份子钱”之苦的出租车司机既享受也抗议,一洗“黑车”之名的专职司机满城 ...
- ArcMAp对线要素进行平滑处(打断)
一:工具简单介绍 -- ArcMAp10.1的高级编辑工具中提供了对线/面要素进行概括/平滑处理的工具. 概括工具.平滑工具分别例如以下:(首先得开启编辑状态 --- 才干够对要素的属性进行更改).选 ...
- 自己定义标签中tagsupport的一些方法
TagSupport生命周期 TagSupport类分别实现了Tag与IterationTag界面,其预设的回传值是:doStartTag()回传 SKIP_BODY.EVAL_BODY_INCLUD ...
- 未能加载文件或程序集“**, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载格式不正确的程序. 原来, ...
- docNet基础学完感想
开学后的一个多月因为要准备acm省赛,所以docnet视频基本没看了!不过,虽然在省赛前每天都在做题,赛前刷了80多题吧!!但是比赛的时候就3题,渣啊!只做出了3个水题,后面两个小时搞两题就是出不来, ...
- objective-C学习笔记(八) 集合类型 Collection Types
OBJC的集合类型: 1.数组 Array 2.Set 3.键值对 Dictionary 数组:OC中的数组被定义为class,引用类型.索引从0开始,访问越界会抛出运行时异常. NSArray的元素 ...
- dubbo架构演变之路
背景 (#) 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时, ...