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 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;
- }
- }
Below points clarify what's wrong with the above program:
- 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 {
-
- }
- <Access-Modifier> set {
-
- }
- }
-
- 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;
- }
- }
- }
- }
Output of the above program,
Program has worked fine for first
Employee object i.e
emp1 as every field was valid, but in case of
emp2 it throws an Exception as
LastName is
Null as inside
LastName property we have written logic to throw an exception if
LastName is
Null.
What problems does the above program solve?
- As _firstName, _lastName, _salary are private it solves the problem of exposing implementation details of class to outside world and hence achieving encapsulation principle.
- Also _firstName, _lastName cannot be set to Null as inside set accessor checked if the value is Null and _salary also cannot be set to minus. Thus we are saving our object from getting corrupted due to invalid data.
| |
| | |
|
|
|
|
|
Text-to-speech function is limited to 200 characters