What are accessors of properties in C#?

CsharpProgrammingServer Side Programming

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;
   }
}
raja
Published on 18-Sep-2018 11:43:52
Advertisements