A property is a member that provides a way to read, write, and manipulate the value of a private field. Properties can be used as if they are public data members, but internally they are special methods called accessors (get, set). We can say properties are syntactic sugar on getter and setter methods which can be used to set or get private fields. Properties are helpful to achieve the encapsulation feature of object-oriented programming.
Why do we need properties?
I will try to explain this with an example.
Brief information about the below program
I will try to explain this with an example.
Brief information about the below program
- I created one Class "Employee" and added three fields _firstName, _lastName, _salary.
- Inside the main method I created an object of "Employee" class and assigned values for three fields.
- namespace PropertiesDeepDive
- {
- static class Program
- {
- private static void Main()
- {
- Employee emp = new Employee();
- emp._firstName = "John";
- emp._lastName = null;
- emp._salary = -1000;
- Console.WriteLine("FirstName:{0}\nLastName: {1} \nSalary: {2}", emp._firstName,emp._lastName, emp._salary);
- Console.ReadLine();
- }
- }
- public class Employee
- {
- public string _firstName;
- public string _lastName;
- public int _salary;
- }
- }
- The very first thing wrong about this program is it is violating Encapsulation principle of OOPS as it is exposing internal details of class to the outside world by making fields public.
- The second problem is, in the real world the last name cannot be null or Salary cannot be negative, but in the above program, we can set _lastName to Null and _salary to negative value thus corrupting object of Employee. Also, we cannot restrict the user from assigning the negative value to _salary or Null value to _lastName to solve these issue C# comes up with Properties member.
Syntax For Property
- <Access-Modifier> <Type> <PropertyName> {
- <Access-Modifier> get {
- /*Get Accesser Logic*/
- }
- <Access-Modifier> set {
- /*Set Accesser Logic*/
- }
- }
- /*Example of property*/
- public int Salary
- {
- get
- {
- return _salary;
- }
- set
- {
- _salary = value;
- }
- }
Property Sytax Explanation
- <Access-Modifier>
Can be Public, Protect, Private which decides accessibility. - <Type>
Can be the value type or reference type (eg Class, Interface, struct or any value types like int) - <PropertyName>
It is the name of the property - get
This is called a get accesser and is used to get the value - set
This is known as a set accesser and is used to set the value
Employee class is modified to solve issues that appeared in the first program as below,
- Changed _firstName, _lastName, _salary fields from the public to private.
- Added three public properties FirstName, LastName and Salary for _firstName, _lastName, _salary fields respectively.
- For FirstName and LastName property Inside set accessor, the check for value is Null and if the value is Null it will throw an exception
- Also added a check for Salary that if the value is less than or equal to zero itthrows an exception that salary cannot be negative
- In the Main class two objects of Employee class emp1 and emp2, for emp1 are assigned correct values for all three fields and in emp2 assigned Null for Lastname and Salary is in the negative
- namespace PropertiesDeepDive
- {
- static class Program
- {
- private static void Main()
- {
- Employee emp1 = new Employee();
- emp1.FirstName = "John";
- emp1.LastName = "Mehar";
- emp1.Salary = 1000;
- Console.WriteLine("FirstName:{0}\nLastName: {1} \nSalary: {2}",
- emp1.FirstName, emp1.LastName, emp1.Salary);
- Employee emp2 = new Employee();
- emp2.FirstName = "John";
- emp2.LastName = null;
- emp2.Salary = -1000;
- Console.WriteLine("FirstName:{0}\nLastName: {1} \nSalary: {2}",
- emp2.FirstName, emp2.LastName, emp2.Salary);
- Console.ReadLine();
- }
- }
- public class Employee
- {
- private string _firstName;
- private string _lastName;
- private int _salary;
- public int Salary
- {
- get
- {
- return _salary;
- }
- set
- {
- if (value < 0)
- throw new Exception("Salary can not be negative");
- _salary = value;
- }
- }
- public string FirstName
- {
- get
- {
- return _firstName;
- }
- set
- {
- if (value == null)
- throw new Exception("FirstName can not be Null");
- _firstName = value;
- }
- }
- public string LastName
- {
- get
- {
- return _lastName;
- }
- set
- {
- if (value == null)
- throw new Exception("LastName can not be Null");
- _lastName = value;
- }
- }
- }
- }

Laxmidhar SahooPosted Nov 28, 2018, 2:05 AM
Thank you very much
Anil KumarPosted Jul 23, 2018, 5:11 AM
In the Main class two objects of Employee class emp1 and emp2, for emp1 are assigned correct values for all three fields and in emp2 assigned Null for Lastname and Salary is in the negative ........But i can see in emp1 and emp2 the values given are same in main method......by executing the above code how the output with last name Mehar came...there is no Value given for lastname is Mehar....pLease clarify me....
Viknaraj ManogararajahPosted Jul 19, 2018, 6:41 AM
Nice Article...........
Shrimant TelgavePosted Jul 4, 2018, 7:17 AM
To understand the encapsulation this blog will help more.