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 ...
随机推荐
- egret命令行编译项目时 版本不对应的问题
egret 命令行编译项目时 如使用 egret build -e 会出现版本不对应的问题 分析原因 A,B项目 A项目使用1.8的egret引擎, B项目使用2.5引擎 但本地引擎升级至2.5.5, ...
- 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动”
在VC++2008的项目中,如何显示地指定要使用的C++库的版本? 开发环境:VS2008 SP1 + Win2003 SP2 因为我的VS2008安装了SP1补丁,所以有了9.0.3 ...
- spring集成mongodb封装的简单的CRUD
1.什么是mongodb MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB是一个介 ...
- java字符串函数及理解
Java中的字符串也是一连串的字符.但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理.将字符串作为内置的对象处理允许Java提供十分丰富的功能特性 ...
- HUD 4473 Exam
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4473 题目意思 定义f(x) = 满足(a * b)|x的有序对(a,b)的个数. 然后输入一个n, ...
- 2.Freshman阶段学习内容的确定
我刷知乎.在知乎上答题的程序员,不是很牛逼就是更牛逼,说起各种系统.各种系统的各种版本.各种语言.数据库.算法.IT届的各种圣战都有板有眼.信手拈来.头头是道,不得不服.这导致了一些非常严重的问题:我 ...
- FusionCharts 学习总结
FusionCharts和Charts一样都是对数据进行统计并绘制成图标的控件,但FusionCharts带有跨浏览器的flash图表组件解决方案,功能更为强大.. 在这里我将采用Js来加载Fusio ...
- 浅谈Servlet(二)
1.forward(请求的转发)和redirect(重定向) 目的:都是为了把一个Servlet的功能,拆分到多个Servlet中,便于后续代码的维护. a.forward(请求转发) (1).如何在 ...
- PHP-购物网站开发设计(一)
2015-07-6 开始使用PHP完成简单购物网站的设计,首先要选择合适的软件平台,所以今天先记录平台的选择与搭建: 我选择使用Apache24 + PHP 5.6 + MySQL 开发环境完成PHP ...
- c#计算文件的MD5值
代码: /// <summary> /// 计算文件的 MD5 值 /// </summary> /// <param name="fileName" ...