Naming Convention

  • PascalCase
    • class names
    • properties
    • methods and
    • constants (such asCalculateTotal ,UserProfile ).
  • camelCase
    • local variables
    • method parameters (for example totalAmount ,userId ).
  • prefix _
    • private fields
    • (optional and project-specific)
      • (for instances _ repository , _ isEnabled ).
  • constant
  • All uppercase with underscores for constants
    • such as MAX_RETRIES ,DEFAULT_TIMEOUT
  • Async
    • Async methods should end with the suffix Async
      • for example SendEmailAsync ,LoadDataAsync to clearly indicate asynchronous behavior.

Variables

Each variable has a scope (where it is accessible) and a lifetime (how long it exists in memory

  • variables declared within a method are local to that method and stored on the stackstored on the stack, making them short-lived and efficient.

C# also supports type inference using the var keyword. Operators

Constants

  • Constants are declared, using the const keyword and must be assigned a value at the time of declaration
  • Constants must be of a [built-in type]() (such as int , double , string , and the list can continue) or, an enum
  • Are always implicitly static, meaning that they belong to the type itself, rather than to any instance.

Readonly

Fields, whose values are assigned only once—either

  • at the point of declaration or
  • within a constructor.

These fields are evaluated at runtime and enforcing immutability after initialization.

Readonly Fields

ideal for values that should remain unchanged after object creation, but may vary depending on constructor parameters, configuration, or calculated logic.

Constant vs Readonly

Since, constants are substituted at compile time, changing a constant’s value in a shared library, requires recompilation of all dependent assemblies, making them suitable only for values that are truly fixed and unchanging.

The readonly keyword is used to declare fields, whose values are assigned only once—either at the point of declaration or within a constructor. Unlike const , which is a compile-time constant, a readonly field is evaluated at runtime. This makes it ideal for values that should remain unchanged after object creation, but may vary depending on constructor parameters, configuration, calculated logic or external values, such as user input, system configuration, or injected dependencies

Note

For values that may change at runtime, but are intended to be immutable once initialized, consider using readonly fields instead. Common readonly candidates

private readonly Guid _id;
private readonly ILogger _logger;
private readonly IRepository _repository;
private readonly string _connectionString;
public readonly string Environment
private readonly IConfiguration _configuration;

Types

C# has two kinds of types: reference types and value types. A value type or a reference type may be a generic type, which takes one or more type parameters. Type parameters can designate both value types and reference types.

C#’s type system is unified such that a value of any type can be treated as an object. Every type in C# directly or indirectly derives from the object class type, and object is the ultimate base class of all types. Values of reference types are treated as objects simply by viewing the values as type object.Values of value types are treated as objects by performing boxing and unboxing operations

Reference Type

  • Variables of reference types store references to their data (known as objects)
  • With reference types, two variables can reference the same object. Therefore, operations on one variable can affect the object referenced by the other variable.
  • A reference type value is a reference to an instance of the type, the latter known as an object.
  • The special value null is compatible with all reference types and indicates the absence of an instance.

Types of Reference Type

Ref

Value Type

  • Variables of value types directly contain their data.
  • With value types, each variable has its own copy of the data. Operations on one variable can’t affect the other variable, except in the case of inref, and out parameter variables.
  • For more information, see inref, and out parameter modifier.

A value type is either a struct type or an enumeration type. C# provides a set of predefined struct types called the simple type_s. The simple types are identified through keywords and contextual keywords.

Types of Value Type

Ref

# Nullable Reference Types

NullReferenceException & Null Safety (.NET 8) Ref

Nullable Reference Types are NOT a new type, nor are they value types

Under the hood, reference types (like string or User) are always reference types. Nullable reference types (string? or User?) are purely a compile-time safety feature enforced by the C# compiler. At runtime, a string? is compiled down to the exact same IL (Intermediate Language) as a regular string. The question mark (?) is just a hint to the compiler to generate warnings if you don’t check for nulls.