C# Property
Property is a named member of a class, structure, or interface. Member variables or methods in a class or structure are called Fields. Properties are extensions of fields and can be accessed using the same syntax. They use accessors to allow the values of private fields to be read, written, or manipulated.
Properties do not determine a storage location. Instead, they have accessors that can read, write, or compute their values.
For example, there is a class named Student with private fields for age, name, and code. We cannot access these fields directly outside the class, but we can have properties to access these private fields.
Accessors
The accessors of a property contain executable statements that help to get (read or compute) or set (write) the property. The accessor declarations can include 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:
using System;
namespace tutorialpro
{
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 student's code, name, and age
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: {0}", s);
// Increase age
s.Age += 1;
Console.WriteLine("Student Info: {0}", s);
}
}
}
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 Properties
Abstract classes can have abstract properties, which should be implemented in the derived class. The following program demonstrates this:
Example
using System;
namespace tutorialpro
{
public abstract class Person
{
public abstract string Name
{
get;
set;
}
public abstract int Age
{
get;
set;
}
}
class Student : Person
{
private string code = "N.A";
private string name = "N.A";
private int age = 0;
// Declare Code property of type string
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare Name property of type string
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare Age property of type int
public override 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 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