What are accessors of properties in C#?


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.

The accessor of a property contains the executable statements that helps in getting (reading or computing) or setting (writing) the property.

Let us see an example of properties in C#.

Example

Declare a code property of type string.

public string Code {
   get {
      return code;
   }
   set {
      code = value;
   }
}

In the same way, declare Age property of type as in the following code snippet.

Example

public int Age {
   get {
      return age;
   }
   set {
      age = value;
   }
}

Updated on: 23-Jun-2020

556 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements