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

转载于:https://www.cnblogs.com/lmfy/p/5109170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值