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的更多相关文章

  1. C# Best Practices - Define Proper Classes

    Application Architecture Define the components appropriately for the application and create project ...

  2. 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 ...

  3. Java 控制反转和依赖注入模式【翻译】【整理】

    Inversion of Control Containers and the Dependency Injection pattern --Martin Fowler 本文内容 Component ...

  4. Martin Fowler关于IOC和DI的文章(原版)

    Inversion of Control Containers and the Dependency Injection pattern In the Java community there's b ...

  5. 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 ...

  6. ExtJS笔记2 Class System

    For the first time in its history, Ext JS went through a huge refactoring from the ground up with th ...

  7. 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 ...

  8. Azure Redis Cache作为ASP.NET Session状态提供程序

    从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...

  9. Inversion of Control Containers and the Dependency Injection pattern

    https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...

随机推荐

  1. 有关extern的用法

    1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言, C++保留了一部分过程式 ...

  2. Windows 安装 psutil

    第一步:下载pustil 网址:https://pipy.python.org  (http://yunpan.cn/cJg8aQpYwqfzh (提取码:7fa7)) 第二步:安装这个就没有什么好说 ...

  3. SMT贴片红胶基本知识

    SMT贴片红胶是一种聚稀化合物,与锡膏不同的是其受热后便固化,其凝固点温度为150℃,这时,红胶开始由膏状体直接变成固体. SMT贴片机装贴贴片具有粘度流动性,温度特性,润湿特性等.根据红胶的这个特性 ...

  4. 预处理指令中#Pragma

    在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的 ...

  5. HDU 5274 Dylans loves tree(树链剖分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...

  6. HDU 5763 Another Meaning(FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5763 [题目大意] 给出两个串S和T,可以将S串中出现的T替换为*,问S串有几种表达方式. [题解 ...

  7. HDU 5758 Explorer Bo(树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次 ...

  8. C模块回调Lua函数的两种方法

    作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di C模块回调Lua函数的两种方法 lua和C通过虚拟栈这种交互方式简单而又可靠,缺点就是C做栈平 ...

  9. 1396 - Most Distant Point from the Sea

    点击打开链接 题意: 按顺序给出一小岛(多边形)的点 求岛上某点离海最远的距离 解法: 不断的收缩多边形(求半平面交) 直到无限小 二分收缩的距离即可 如图 //大白p263 #include < ...

  10. 在头文件声明全局变量和创建extern

    在头文件声明全局变量和创建extern 问题: 是否有简单的方法,在头文件里定义我的变量,同时也为它们创建extern定义? 回答: 是的.尽管这不是必需的,使用正确的宏和一个头文件还是很容易实现的. ...