C# Best Practices - Define Fields Appropriately

时间:2021-03-17 20:59:32

Backing Fields

private string description;
private int productId;

Features

A variable in a class

Holds data for each object

Object's data is only accessiable to that object

Fields are private.

Accessible outside of the class through property getters and setters

private string description = "coming soon";

Optional accessibility modifier + Data type + Name + Optional initialization

Best Practices

Do:

Define a meaningful name

Using camelCasing

Keep fields private

Use properties to provide access to the fields

Avoid:

Single character name

Abbreviations

Initialize to the field's default value (*)

Nullable Types

private decimal? cost;
private DateTime? availabilityDate;

Allow definition of a value OR null

Specified with a "?" suffix on the type

Distinguishes "not set" from the default value

Best Practices

Do:

Use on simple types to distinguish "not set" and "default value"

Use properties of the type such as HasValue and Value as needed

Avoid:

Using them if not necessary

Constants

public const double Pi = 3.14;
public const int Red = 0xFF0000;

Define in a class

Holds a hard-coded value that does not change

Must be assigned to an expression that can be fully evaluated at compile time

Think of a constant as a "compile-time" constant value

Are static

Optional accessibility modifier + "const" keyword + Data type + Name + Assigned Value

Best Practices

Do:

Define a meaningful name

Use PascalCasing

Use for compile-time values that will never change

Avoid:

Single character name

Abbreviations

All upper case

For fields that could change over time

Read-Only Fields

public readonly string DefaultMeasure = GetDefaultMeasure();
public readonly decimal MinimumPrice;
public Product()
{
MinimumPrice = RetrieveMinimumPrice();
}

A variable in a class

Holds a value that is initialized and then not changed

Must be initialized (In the declaration Or in a constructor)

Think of a read-only field as a "runtime" constant value

Optional accessibility modifier + Optional "static" keyword + "readonly" keyword + Data type + Name + Assigned value

Constant vs. Read-Only

Constant Field:

Compile-time constant

Assigned to an expression evaluated at compile time

Assigned on declaration

Only number,Boolean or string

Always static

Read-Only Field:

Runtime constant

Assigned to any valid expression at runtime

Assigned on declaration or constructor

Any data type

Optionally static

FAQ

1.Explain the data encapsulation principle

An object's data should be accessible only to the object

Backing fields containing in the object data should be marked as private

2.What's backing field?

A variable in a class used to retain each object's data.

3.When should you use a back field?

For every data retained field for an object

4.When should you use a constant?

When defining a field with a simple data type that will never change.

5.When should you use a read-only field?

When defining a field that is initialized from a file,table,or code but should not then be changed anywhere else in the application.

6.What is the difference between a constant and a read-only field?

A constant is static, assigned in the declaration, assigned to an expression that is fully evaluated at compile time.

A read-only field can be static or non-static,assigned in the declaration or constructor, assigned to any valid expression.