Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A public property in C# is a member of a class, struct, record, or interface that provides controlled read and/or write access to an object’s instance state or a type’s static state. It combines the syntax of a field with the semantics of a method by encapsulating data behind get, set, or init accessor blocks. The public access modifier declares that the property is accessible to any code that can access the property’s containing type. A member’s accessibility is strictly capped by its containing type’s accessibility; therefore, a public property inside an internal class remains completely inaccessible to referencing assemblies.

Auto-Implemented Properties

When custom logic is not required within the accessors, C# provides auto-implemented properties. The compiler automatically generates a hidden, anonymous private backing field to store the data.
public string Identifier { get; set; }

Explicit Backing Fields (Full Properties)

When you need to intercept data during retrieval or assignment, you define a full property with an explicit private backing field. The set and init accessors utilize an implicit parameter named value, which holds the data being assigned.
private int _capacity;

public int Capacity
{
    get 
    { 
        return _capacity; 
    }
    set 
    { 
        // 'value' is a contextual keyword representing the assigned data
        _capacity = value; 
    }
}

Accessor Types

A public property can implement the following accessors to provide read-only, write-only, or read-write access:
  • get: Executes when the property is read. It must return a value matching the property’s type.
  • set: Executes when the property is assigned a new value.
  • init: A specialized setter introduced in C# 9.0 that restricts assignment to the object’s initialization phase (object initializers or constructors), rendering the property immutable thereafter.
public DateTime CreatedAt { get; init; }

Expression-Bodied Properties and Accessors

For properties or accessors containing a single expression, C# supports expression body definitions using the => (lambda) operator. An expression-bodied property omits the get keyword entirely and is implicitly read-only. Alternatively, full properties can utilize expression-bodied get and set accessors.
private string _prefix;
private string _suffix;

// Expression-bodied property (implicitly read-only, omits 'get' keyword)
public string Combined => $"{_prefix}-{_suffix}"; 

// Expression-bodied accessors (C# 7.0+)
public string Prefix
{
    get => _prefix;
    set => _prefix = value;
}

Static Properties

Properties can be declared with the static modifier. A public static property manages state at the type level rather than the instance level, making it accessible via the type name itself.
public static int GlobalInstanceCount { get; private set; }

Asymmetric Accessor Accessibility

While the property itself is declared public, you can apply a more restrictive access modifier to an individual accessor to limit mutation or retrieval scopes. The modifier applied to the accessor must be strictly more restrictive than the property’s declared accessibility. Under C# compiler rules, when defining asymmetric accessibility, you are only allowed to apply an access modifier to one of the accessors (either get or set/init), not both. Specifying modifiers on both accessors results in a compiler error (CS0274).
// Globally readable (subject to type visibility), but only mutable from within the declaring type
public double Temperature { get; private set; }
Master C# with Deep Grasping Methodology!Learn More