Njeri Muriithi
PhoenixTech_ke

Follow

PhoenixTech_ke

Follow
C# Properties

C# Properties

Njeri Muriithi's photo
Njeri Muriithi
·May 5, 2022·

1 min read

C# properties are statements where you set or get data. They make it possible to access and change data from private fields by defining a public get and set method. It is a method that defines getters and setters. They are mostly used to implement encapsulation which means hiding unwanted data to users.

  • Get method is defined as as read only meaning data cannot be modified and its often used for variables that are initialized. If you do not want the values to be changed use the get method.

  • Set method is defined as read and write where data can be accessed and modified outside the class.

class Program{
private string _name;
public string Name
{
get {return _name;}
set{_name= value;}
}

Example : Set the Title of the car using get and set method.

properties.png

The private field car title is accessed and modified by defining the public access modifier which allows outside classes to read and write data or access and modify the title.

output

propoutput.png

 
Share this