Csharp Property
Properties in C# are members of a class or struct used for encapsulating data. They provide a way to define rules for accessing and setting class members, typically used to hide the internal implementation details of fields while providing a mechanism to control data access.
A property can be thought of as a wrapper around a field, typically consisting of `get` and `set` accessors.
A property does not determine a storage location. Instead, they have **accessors** that read, write, or compute their value.
For example, there is a class named `Student` with private fields `age`, `name`, and `code`. We cannot access these fields directly outside the scope of the class, but we can have properties that access these private fields.
### Basic Syntax
```csharp
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
In the code above, the `Name` property encapsulates the private field `name`. The `get` accessor is used to retrieve the field value, while the `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 don't need to explicitly define a field.
```csharp
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.
```csharp
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.
```csharp
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.
```csharp
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 dependent on a field.
```csharp
public class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
The **accessors** of a property contain executable statements that help in getting (reading or computing) or setting (writing) the property. An accessor declaration can contain a `get` accessor, a `set` accessor, or both. For example:
```csharp
// Declare a property of type string called Code.
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a property of type string called Name.
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a property of type int called Age.
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
The following example demonstrates the usage of properties:
## Example
```csharp
using System;
namespace
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a property of type string called Code.
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a property of type string called Name.
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a property of type int called Age.
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 student's code, name, and age
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 above code 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 classes can have abstract properties, which should be implemented in derived classes. The following program illustrates this:
## Example
```csharp
using System;
namespace
{
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 above code 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
YouTip