C# Properties (Property)
Properties in C# are members of classes and structs used to encapsulate data. They provide a way to define the access and setting rules for class members, typically hiding the internal implementation details of fields while providing a mechanism to control data access.
A property can be seen as a wrapper around a field, typically consisting of get and set accessors.
Properties do not determine a storage location. Instead, they have accessors that can read, write, or compute their values。
For example, consider a class named Student with private fields age, name, and code. We cannot access these fields directly from outside the class, but we can have properties that access these private fields.
Basic Syntax
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
In the code above,Name Properties encapsulate private fields name。get The get accessor is used to retrieve the field value, while the set set accessor is used to set the field value.
Auto-Implemented Properties
If you only need a simple property, C# allows the use of auto-implemented properties, so you do not need to explicitly define a field.
public class Person
{
public string Name { get; set; }
}
In this case, the compiler automatically generates a private anonymous backing field for the Name property to store the value.
Read-Only Properties
If you only need a read-only property, you can omit the set accessor.
public class Person
{
public string Name { get; }
public Person(string name)
{
Name = name;
}
}
Write-Only Properties
Similarly, if you only need a write-only property, you can omit the get accessor.
public class Person
{
private string name;
public string Name
{
set { name = value; }
}
}
Custom Logic
You can include custom logic in the get and set accessors.
public class Person
{
private string name;
public string Name
{
get { return name; }
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("Name cannot be empty.");
name = value;
}
}
}
Computed Properties
Properties can also be computed and not rely on a field.
public class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
Accessors
The **accessors** of a property contain executable statements that help get (read or compute) or set (write) the property. An accessor declaration can contain a get accessor, a set accessor, or both. For example:
// Declare a Code property of type string
public
string
Code
{
get
{
return
code
;
}
set
{
code
=
value
;
}
}
// Declare a Name property of type string
public
string
Name
{
get
{
return
name
;
}
set
{
name
=
value
;
}
}
// Declare an Age property of type int
public
int
Age
{
get
{
return
age
;
}
set
{
age
=
value
;
}
}
Example
The following example demonstrates the use of properties:
Example
using
System
;
namespace
runoob
{
class
Student
{
private
string
code
=
"N.A"
;
private
string
name
=
"not known"
;
private
int
age
=
0
;
// Declare a Code property of type string
public
string
Code
{
get
{
return
code
;
}
set
{
code
=
value
;
}
}
// Declare a Name property of type string
public
string
Name
{
get
{
return
name
;
}
set
{
name
=
value
;
}
}
// Declare an Age property of type int
public
int
Age
{
get
{
return
age
;
}
set
{
age
=
value
;
}
}
public
override
string
ToString
(
)
{
return
"Code = "
Code
", Name = "
Name
", Age = "
Age
;
}
}
class
ExampleDemo
{
public
static
void
Main
(
)
{
// Create a new Student object
Student s
=
new
Student
(
)
;
// Set the code, name, and age of student
s
.
Code
=
"001"
;
s
.
Name
=
"Zara"
;
s
.
Age
=
9
;
Console
.
WriteLine
(
"Student Info: {0}"
, s
)
;
// Increase the age
s
.
Age
+=
1
;
Console
.
WriteLine
(
"Student Info: {0}"
, s
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10
Abstract Properties
An abstract class can have abstract properties, which should be implemented in derived classes. The following program illustrates this:
Example
using
System
;
namespace
Runoob
{
public
abstract
class
Person
{
public
abstract
string
Name
{
get
;
set
;
}
public
abstract
int
Age
{
get
;
set
;
}
}
class
Student
:
Person
{
// Declare auto-implemented properties
public
string
Code
{
get
;
set
;
}
=
"N.A"
;
public
override
string
Name
{
get
;
set
;
}
=
"N.A"
;
public
override
int
Age
{
get
;
set
;
}
=
0
;
public
override
string
ToString
(
)
{
return
$
"Code = {Code}, Name = {Name}, Age = {Age}"
;
}
}
class
ExampleDemo
{
public
static
void
Main
(
)
{
// Create a new Student object
Student s
=
new
Student
{
Code
=
"001"
,
Name
=
"Zara"
,
Age
=
9
}
;
Console
.
WriteLine
(
"Student Info:- {0}"
, s
)
;
// Increase the age
s
.
Age
+=
1
;
Console
.
WriteLine
(
"Student Info:- {0}"
, s
)
;
Console
.
ReadKey
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10