C# Property
Properties in C# are members of classes and structs used to encapsulate data. They provide a way to define access and assignment rules for class members, typically used to hide the internal implementation details of fields while providing a mechanism for controlling data access.
A property can be viewed as a wrapper around a field, usually consisting of get and set accessors.
Properties do not determine storage locations. Instead, they have accessors that can read, write, or compute their values.Accessors。
For example, there is a class named Student with private fields age, name, and code. We cannot access these fields directly 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 above code,NameProperties encapsulate private fields.name。getAn accessor is used to obtain the value of a field, whilesetan accessor is used to set the value of a field.
Auto-implemented properties
If you only need a simple property, C# allows you to use 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 field for the Name property to store the value.
Read-only property
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 property
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 property
Properties can also be computed and do not depend on fields.
public class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
Accessors
of the PropertyAccessorAccessors 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:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declares the 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 usage of properties:
Example
namespace example
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declares the Code property of type string
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declares the 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 student's code, name, and age
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: {0}", s);
// Increment age
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following results:
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
Abstract Properties
Abstract classes can have abstract properties, which should be implemented in derived classes. The following program illustrates this:
Example
namespace Example
{
public abstract class Person
{
public abstract string Name { get; set; }
public abstract int Age { get; set; }
}
class Student : Person
{
// Declare an auto-implemented property
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);
// Increment age
s.Age += 1;
Console.WriteLine("Student Info:- {0}", s);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following results:
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10other extensions