C# Best Practices - Building Good Classes

时间:2021-08-21 19:57:04

Building a Class

The last four refer as members

C# Best Practices - Building Good Classes

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

C# Best Practices - Building Good Classes

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