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 ...
随机推荐
- Web开发者需具备的8个好习惯
优秀的Web开发人员工作效率更高,因为他们拥有丰富的经验和良好的习惯.作者Gregor Dorfbauer分享了用于Web开发中的8个好习惯,这些良好的工作习惯不仅能提高效率,还能让您创建更加优秀的应 ...
- SQL Server 通配符为目标字符的查找
create table t(x int identity(1,1) primary key,v nvarchar(32));go insert into t(v) values('this is % ...
- RHEL4-Partition Image系统备份(软件版)
对于BBS,或Apache,PHP等相关网页的程序 备份: 1)/var/www/html目录,里面有PHP所写成的网页.此网页主要功能是从资料库中读取由信件存入的文章,或是使用者选择由网页输入资料时 ...
- 三种尺寸:手机SIM卡使用指南
毫无疑问目前卖的最火的手机非iPhone 5s莫属,相信仍有不少网友目前处于观望之中,由于iPhone 5s和iPhone 5c采用与iPhone相同的Nano-SIM卡,因此不少新用户在使用之前也徒 ...
- 强化:把treeview的QString路径转换为QModelIndex节点,有了节点就什么都好办了
http://doc.qt.io/qt-4.8/qdirmodel.html#index-2 甚至还能直接调用setData: setData(const QModelIndex &index ...
- one Infos
Backend Server is still running (PID:). Please try 'euc-server stop' first. (原因很简单,进程控制脚本无restart函数功 ...
- 如何注册Uber司机,加入uber(全国版最新最详细注册流程)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 取文件的大小 (KB,MB,GB...)
取文件的大小 (KB,MB,GB...) 2种方式: VB 和 C# 1, VB Public Function GetFileSize(ByVal iFileSizeKB As Long) As ...
- C# 动态载入Dll
1.新建測试dll及方法,用vs2010新建winform程序,详细代码例如以下: using System; using System.Collections.Generic; using Syst ...
- CSS换行2
1.可以使用强制换行符号<br />换行.如果在一个文章里可以在文章需要换行的地方加入<br />即可实现自动换行-常说的小换行,与换行前没有间隔.实例如下图 换行说明图无间隔 ...