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. delete了,析构函数却没有调用

    析构函数在对象的生命结束时,会自动调用,大家所熟知的智能指针就是根据析构函数的这种特性而实现的,包括Qt的内存管理机制,也都是利用了析构函数的这一机制来实现的.c++创始人Bjarne Stroust ...

  2. BZOJ 1037 [ZJOI2008]生日聚会Party(单调DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1037 [题目大意] 现在有n个男生,m个女生排成一行,要求不存在一个区间男女之差大于k ...

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

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

  4. ios网络学习------8 xml格式数据的请求处理 用代码块封装

    #pragma mark 载入xml - (void)loadXML { //获取网络数据. NSLog(@"load xml"); //从webserver载入数据 NSStri ...

  5. Android 匿名共享内存C接口分析

    在Android 匿名共享内存驱动源码分析中详细分析了匿名共享内存在Linux内核空间的实现,虽然内核空间实现了匿名共享内存,但仍然需要在用户空间为用户使用匿名共享内存提供访问接口.Android系统 ...

  6. 安卓中onBackPressed ()方法的使用

    一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...

  7. 1.4. chromium源代码分析 - chromiumframe - 消息系列

    Message framework 是对消息循环的封装和扩展,Chromium在消息循环中增加处理内部任务的工作.将内部工作处理寄生在Windows的消息循环中,会有一个问题,就是没有Windows自 ...

  8. MicroStrategy笔试

    1. coding判定二叉树是否是有序二叉树 2. 一个有序数组A(buffer足够大),和一个有序数组B,设计算法,merge两个数组后有序,不使用任何额外的内存空间 3. 100个点灯问题,初始状 ...

  9. chroot命令

    CHROOT就是Change Root,也就是改变程序执行时所参考的根目录位置.通过chroot机制来更改某个进程所能看到的根目录,即将某进程限制在指定目录中,保证该进程只能对该目录及其子目录的文件有 ...

  10. 01-C语言基本知识

    目录: 一.C语言基本知识 二.C语言概述 回到顶部 一.C语言基本知识 1 语言背景 1946年,美国冯·诺依曼第一台计算机. 四大部分:中央处理器(控制器,运算器),存储器,输入设备,输出设备. ...