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 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 The following example demonstrates the use of Property: When the above code is compiled and executed, it produces the following results: Abstract classes can have abstract properties, which should be implemented in derived classes. The following procedure illustrates this: When the above code is compiled and executed, it produces the following results:
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. 1.49.1. Accessors #
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;
}
}
1.49.2. Example #
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();
}
}
}
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10
1.49.3. Abstract Properties #
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();
}
}
}
Student Info: Code = 001, Name = Zara, Age = 9
Student Info: Code = 001, Name = Zara, Age = 10