C# property
Properties are named members of classes, structures and interfaces. A member variable or method in a class or structure is called a Field. The Property is an extension of the Field and can be accessed using the same syntax. They use accessors to make private domain values read, written or manipulated.
Property does not determine the storage location. Instead, they have an accessor that reads, writes or calculates their values.
For example, there is a person named the private domain of Student
with the 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 domains.
Accessors
The accessor of a property contains executable statements that help you get (read or evaluate) or set (write) the property. The accessor declaration can contain a get
accessor, a set
accessor,or both. For example:
// Declare a Code attribute of type string
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare the Name attribute of type string
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare Age attribute of type int
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
Example
The following example demonstrates the use of Property:
Example
using System;
namespace runoob
{
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
// Declare a Code attribute of type string
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare the Name attribute of type string
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare Age attribute 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 the student
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 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 procedure 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
{
private string code = "N.A";
private string name = "N.A";
private int age = 0;
// Declare a Code attribute of type string
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare the Name attribute of type string
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare Age attribute 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 code, name, and age of the student
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 results:
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10