Properties in C# full Explanation
Properties in C# provide a way to encapsulate the
state (data) of an object and define the logic for accessing and modifying that
state. They combine the functionality of fields and methods into a single
interface. Here's a full explanation of properties in C#:
1.
Definition:
v A
property is a member of a class or struct that encapsulates a getter (accessor)
and/or a setter (mutator) method to access and modify the underlying data
(state) of an object.
v Properties
provide a way to expose the internal state of an object while controlling
access to it and applying logic if needed.
2.
Syntax:
v Properties
are declared within a class or struct using a combination of a get accessor
(optional), a set accessor (optional), and a property name.
v The
get accessor retrieves the property value, and the set accessor sets the
property value.
v The
property name follows the naming conventions for variables and methods.
v Example:
public string Name
{
get { return _name; }
set { _name = value; }
}
3.
Access Modifiers:
v Properties
can have Different access modifiers to control their visibility and
accessibility.
v The
most common access modifiers used for properties are `public`, `private`,
`protected`, and `internal`, which have the same meaning as for fields and
methods.
4.
Backing Field:
v A
property typically relies on a private field (backing field) to store its
underlying data.
v The
backing field is accessed and modified within the property's get and set
accessors.
v By
convention, the backing field is often named with an underscore prefix to Differentiate
it from the property.
v Example:
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
5.
Auto-implemented Properties:
v C#
provides a shorthand syntax for creating properties called auto-implemented
properties.
v Auto-implemented
properties automatically generate a hidden backing field and handle the get and
set functionality internally.
v They
are useful when you don't need any additional logic in the property's
accessors.
v Example:
public string Name { get; set; }
6.
Read-only and Write-only Properties:
v Properties
can be defined as read-only (only a getter) or write-only (only a setter).
v Read-only
properties allow you to get the value but not modify it after initialization.
v Write-only
properties allow you to set the value but not retrieve it.
v Example
of a read-only property:
public int Age { get; } //
Read-only property
7.
Property Usage:
v Properties
can be accessed and modified like fields, using dot notation.
v Example
usage:
MyClass myObj = new MyClass();
myObj.Name = "John"; // Setting the value of the property
string name = myObj.Name; // Getting the value of the property
Properties offer advantages over direct field access
because they allow you to add logic, validation, and additional behavior to the
reading and writing of data. They provide an interface to safely expose and
manipulate the internal state of an object. By using properties, you can
enforce encapsulation and control access to the data in your C# classes and
structs.
0 Comments