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 ...
随机推荐
- MySQL Mysqlslap
在mysql5.1以后的版本:客户端带了个工具mysqlslap可以对mysql进行压力测试: 可以使用mysqlslap --help来显示使用方法: Default options are rea ...
- SQL Server 数据库所有者
1. 数据库所有者应当永远是 sa 用户 2. 改变数据库的所有者 alter authorization on database :: databaseName to sa; -- 这一句话把数据库 ...
- crtmpserver初探
前言 Adobe的FMS(Flash Media Server)是很好用.但对应着分级授权的是money和有限功能开放.商业的东西既然用不起,也阻碍了我们的技术进步,那就只能求助于开源社区 ...
- ACCP6.0 教程课件,可用
下载地址 求分 http://download.csdn.net/detail/qq873113580/6038955 包含ACCP6.0所有,是我朋友的自己保存的,请大家不要修改密码,谢谢 下面就是 ...
- 编译器DIY——词法分析
在上一篇文章中已经介绍了读文件的操作,那么这一篇文章中将会细致解释词法分析. 在源文件里解析出的单词流必须识别为保留字,标识符,常量,操作符和界符五大类 1.显然我们须要列举出全部的保留字,而这里与保 ...
- Python BeautifulSoup4 使用指南
前言: 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把Beautifu ...
- 【整理】Object-C中的属性(Property)的Setter:assign,copy,retain,weak,strong之间的区别和联系
iOS编程过程中,经常看到一些属性前面有些修饰符,比如copy,retain等. 这些关键字,是Object-C语言中,对于Property的setter. Mac官网: The Objective- ...
- php中0,空,null和false的区别
<? $str1 = null; $str2 = false; echo $str1==$str2 ? ‘相等’ : ‘不相等’; $str3 = ""; $str4 = 0 ...
- Java "JSON中无分隔符日期字符串处理"
Json 中日期类型数据处理,服务端传输的日期没有分隔符,一般格式就两种,[20151212121212]即yyyyMMddhhmmss和[121212]hhmmss import java.text ...
- Codecademy学习打卡1
————————————————————————— 想学习编程,并且打算“闭门造车”式的开启我的自学生涯. 前段时间买了一门厚重的“Java从入门到精通”.或许是对代码,电脑编程环境的陌生,再加上纯小 ...