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 behindDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.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. Theset and init accessors utilize an implicit parameter named value, which holds the data being assigned.
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.
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.
Static Properties
Properties can be declared with thestatic modifier. A public static property manages state at the type level rather than the instance level, making it accessible via the type name itself.
Asymmetric Accessor Accessibility
While the property itself is declaredpublic, 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).
Master C# with Deep Grasping Methodology!Learn More





