C# Best Practices - Building Good Classes
Building a Class
The last four refer as members

Signature
Accessiblity modifier (Default:internal)
class keyword
Class Name
XML documents comments
Fields
A variable in the class
Hold the data
Properties
Getter and Setter functions
Guard access to the fields (backing fields)
Methods
Function
Behaviors and Operations
Class Best Practices
Do:
Class naming: Define the meaningful name, Use a noun, Use PascalCasing
Add XML document comments
Ensure the class has well-defined purpose
Create one class per code file
Use properties to encapsulate fields
Use methods for logic
Add properties above the methods
Avoid:
Class naming: Abbreviations Prefixes Underscores
Large class
Constructor
Basic Constructor
Special method in the class
Executed when instance is created
Named with the class name
Default constructor has no parameters
Not required
Parameterized Constructor
Defines parameters to initialize the instance
Constructor overloading
Use "this" to invoke another constructor
public Product()
{
} public Product(string productName) : this()
{
this.ProductName = productName;
}
Constructor chaining
Minimizes repeatable code
Constructor Best Practice
Do:
Consider providing the default constructor
Consider providing a parameterized constructor to initialize the minimum properties for a valid object
Name the parameters the same name as the related properties
Avoid:
Performing too much work
Namespaces
Automatically added around every class
Same name as the project
Used to provide a unique address and organize classes into a logic hierarchy

Namespace Best Practice
Do:
Follow <company>.<technology>.<feature>
Acme.Wpf.Pm
Microsoft.Office.Interop.PowerPoint
Use PascalCasing
Avoid:
Using System
Using a class name
Namespace: Acme.Biz.Product , Class: Acme.Biz.Product.Product
Static Class
static keyword in the signature
Only static members
Can not instantiate a static class (Use the class name instead)
Provides a container for utility features (like Email or Logging)
Static Class Best Practice
Do:
Use sparingly (only for supporting classes)
Use for common code library components when needed
Avoid:
Using as a miscellaneous bucket (Every class should have a purpose)
Singleton
public class User
{
private static User instance;
private User() {}
public static User Instance
{
get
{
if (instance == null)
{
instance = new User();
}
return instance;
}
}
}
Provides only one instance
Private constructor
Static property provides the one instance
Instance accessed with User.Instance
Advantages of a Singleton vs. Static Class
A singleton has an instance - Can be passed to other code as needed
A singleton can have child objects - Example:User instance has a set of roles associated with it.
A singleton support object-oriented programming features
It can implement an interface. It can be inherited from.
FAQ
1.What is the difference between a property and a method?
Properties are the gate-keepers,providing access to the data.
Methods are the operations.
2.What is constructor?
A method executed when an instance is created from a class
3.What is the purpose of a namespace?
Organize classes into a logic hierarchy
Prevent class name collisions
4.What is a static class?
A class that cannot be instantiated
It is best for use with common code libraries
5.What is a singleton?
A class that provides a single instance of itself
6.What is the difference between a static class and a singleton?
A static class cannot be instantiated
A singleton can instantiate itself and provide that instance
C# Best Practices - Building Good Classes的更多相关文章
- C# Best Practices - Define Proper Classes
Application Architecture Define the components appropriately for the application and create project ...
- Inversion of Control Containers and the Dependency Injection pattern(转)
In the Java community there's been a rush of lightweight containers that help to assemble components ...
- Java 控制反转和依赖注入模式【翻译】【整理】
Inversion of Control Containers and the Dependency Injection pattern --Martin Fowler 本文内容 Component ...
- Martin Fowler关于IOC和DI的文章(原版)
Inversion of Control Containers and the Dependency Injection pattern In the Java community there's b ...
- Inversion of Control Containers and the Dependency Injection pattern--Martin Fowler
原文地址:https://martinfowler.com/articles/injection.html n the Java community there's been a rush of li ...
- ExtJS笔记2 Class System
For the first time in its history, Ext JS went through a huge refactoring from the ground up with th ...
- Core Java Volume I — 4.1. Introduction to Object-Oriented Programming
4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is th ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...
随机推荐
- 四轴飞行器1.4 姿态解算和Matlab实时姿态显示
原创文章,欢迎转载,转载请注明出处 MPU6050数据读取出来后,经过一个星期的努力,姿态解算和在matlab上的实时显示姿态终于完成了. 1:完成matlab的串口,并且实时通过波形显示数据 2:添 ...
- SQL Server 排名函数实现
在SQL Server 中有四大排名函数分别是: 1.row_number() 2.ntile() 3.rank() 4.dense_rank() -------------------------- ...
- Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh
Hyperpolyglot Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh grammar | quoting and escaping | charactersvar ...
- CDialogSK - A Skinnable Dialog Class
Introduction This class is derived from the MFC CDialog. It supports the following features :- If ru ...
- 动态规划以及在leetcode中的应用
之前只是知道动态规划是通过组合子问题来解决原问题的,但是如何分析,如何应用一直都是一头雾水.最近在leetcode中发现有好几道题都可以用动态规划方法进行解决,就此做下笔录. 动态规划:应用于子问题重 ...
- 普通IT和文艺IT工程师的区别
在一个UITableView的editing设置的方法实现过程中,我想到两种写法,顺便想了一下两种方法的区别.觉得这时一个普通IT工程师和NB工程师的区别一个有趣的印记. 您通常时怎么去实现的呢? - ...
- C#中数组、ArrayList和List三者的区别(转)
好东西,总结的很到位,收藏了! 源地址:http://blog.csdn.net/zhang_xinxiu/article/details/8657431
- nginx 使用安装问题及解决方案
如何安装清参考:http://www.runoob.com/linux/nginx-install-setup.html 可以先不用配置,等理解后在配置. 在启动nginx时,出现提示: nginx: ...
- URL地址的编码和解码问题
编码:encodeURIComponent() 方法:把URI字符串采用 UTF-8编码格式转化成escape格式的字符串.与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字 ...
- (Problem 41)Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...