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. 尼康D5100使用设置及技巧,同样也适用尼康D5200

    尼康D5100使用设置及技巧,同样也适用尼康D5200,希望对新手能有点帮助. 一.设置 1.优化校准:可以在menu菜单中找到它,一般使用"标准"就可以,建议将"标准& ...

  2. uva 10779 Collectors Problem 网络流

    链接 一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的. 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, ...

  3. sql2012管理

    一.还原完整备份的语法如下: RESTORE DATABASE { database_name | @database_name_var }                  --数据库名 [ FRO ...

  4. Android 开发UI牛博[转]

    Android 新兴的UI模式——侧边导航栏 侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个 ...

  5. SQLServer 2008 :error 40 出现连接错误

      在与SQLServer建立连接时出现与网络相关的或特定与实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接.(provide:命名管道提供程序,e ...

  6. ios 设备用jquery live绑定 click 事件不管用

    问题描述:用js拼接的html追加到页面,然后用 live 绑定click事件不起作用 解决办法:1.直接在标签写onclick事件   2.给需要绑定的标签添加css样式{cursor:pointe ...

  7. SQL Server索引进阶:第九级,读懂执行计划

    原文地址: Stairway to SQL Server Indexes: Level 9,Reading Query Plans 本文是SQL Server索引进阶系列(Stairway to SQ ...

  8. ASP.NET页面之间数据传递的几种方法

    1)Request.QueryString   在ASP时代,这个是较常用的方法,到了ASP.NET,好像用的人不多了,但是不管怎么说,这是一个没有过时,且很值得推荐的方法,因为不管是ASP还是ASP ...

  9. 2015.8.3 Java

    今天继续学习Java  用的是eclipse IDE 这个ide怪怪的,但是有一个很方便的功能 就是通过右键选择source 可以点击Generate Getters and Setters生成属性的 ...

  10. C++中 指针 与 引用 的区别

    四点区别:可否为空,可否修改,使用时是否需要判断,使用场景 非空区别. 引用必须指向某个对象,而指针可以指向空. 可修改区别. 引用总是与初始化时的那个对象绑定,不可变更:指针可以重新赋值,指向另外一 ...