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 ...
随机推荐
- 从零开始PHP学习 - 第二天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- U盘检测软件:ChipGenius,MyDiskTest
几年前的事情了.有一次去北邮玩,看到校园里有卖U盘的摊位,问了问价格,8GB的金士顿U盘99块钱.正好头一天有个同事跟我说最近U盘降价了,8GB才99,于是心里一痒痒就买了一个.回来用着就感觉不对劲, ...
- sqlite 查询数据 不用回调
int main( void ){ sqlite3 *db=NULL; char *zErrMsg = 0; int rc; //打开数据库连接 rc = sqlite3 ...
- 三种客户端访问wcf服务端的方法 C#
原文 http://blog.csdn.net/zlj002/article/details/7914556 string jsonstr = String.Empty; string url = & ...
- Google市场推广统计
Google Play作为Android最大的应用市场,也存在这推广等常用的行为,那么如何统计呢,Google Analytics SDK或者其他的SDK都提供了方法,实际上是可以不需要任何sdk,完 ...
- javascrip cookie
首先要明白一下cookie的概念.由于HTTP协议是一种无状态协议,也就是说一旦server和client的数据交换完成后,他们之间的连接就会被断开.再次交换数据的时候就须要再次建立连接.这就意味着s ...
- Android内存泄漏分析及调试
尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/13017999 此文承接我的另一篇文章:Android进程的内存管理分析 首先 ...
- POJ 3623 Best Cow Line, Gold(字符串处理)
题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再 ...
- hibernate通过配置文件生成数据库信息
hibernate可以通过配置文件在数据库生成相应的数据库信息.也可以把数据库的信息生成相应的代码(实体类操作类和映射文件) 下面是通过代码默认对hibernate.cfg.xml信息在数据库生成信息 ...
- 零行代码为App添加异常加载占位图
前文提要 近期准备重构项目,需要重写一些通用模块,正巧需要设置App异常加载占位图的问题,心血来潮设想是否可以零行代码解决此问题,特在此分享实现思路. 思路分享 对于App占位图,通常需要考虑的控件有 ...