csharp_properties
csharp_properties
Properties are named members of classes, structures, and interfaces. Member variables or methods in a class or
structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use
accessors through which the values of the private fields can be read, written or manipulated.
Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.
For example, let us have a class named Student, with private fields for age, name and code. We cannot directly access
these fields from outside the class scope, but we can have properties for accessing these private fields.
Accessors
The accessor of a property contains the executable statements that helps in getting (reading or computing) or setting
(writing) the property. The accessor declarations can contain a get accessor, a set accessor, or both. For example:
Example:
using System;
class Student
{
When the above code is compiled and executed, it produces following result:
An abstract class may have an abstract property, which should be implemented in the derived class. The following
program illustrates this:
using System;
public abstract class Person
{
public abstract string Name
{
get;
set;
}
public abstract int Age
{
get;
set;
}
}
class Student : Person
{
When the above code is compiled and executed, it produces following result: