C# Properties

tutorial and example
tutorial and example

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all the fields of the class private and use only properties for getting and setting values. Prior to this, we were using method for this purpose.

We can implement the logic also while setting values. Properties in C#, are read only or write only. They don’t have storage location.

In C#, Properties are either read only or write only. They have accessors which are used to set and get values.

Let’s see an example where we are using Set and Get for getting and setting values.

Output

C# properties Example: using logic while setting value                                        

Output