Properties - DotNet and DataBase

Thursday, 13 September 2018

Properties




In C#. properties are the extension of data fields. In the object-oriented programming language, encapsulation and hiding are the pillars.  By using various access modifiers like private, public, protected, internal etc, we can control the accessibility of the class members.
Usually, inside a class, we declare a data field as private and provide a set of public SET and GET methods to access the data fields. Since this is not a good programming practice to give direct accessibility of data fields outside the class.

Let us suppose we have an employee class with three fields, Empid, name and company name.
If we give direct access to these three fields outside the class, there is a chance to get false data as below.


In any organization, the employee never has an ID with the negative number as well null value to the Employee name. So, while supplying the required data it is mandatory to identify whether the data is valid or not.
So, by applying data encapsulation and hiding techniques, we should stop direct access to the data fields and provide through properties as below



Using GET() and SET() methods we can read value and assign to the field members.

GET() represents read property and SET() represents write property.
A property with GET() only,  it is a read-only property. We can't manipulate the value on it. In the above example, Company name is common for all employees across the organization. So, we should stop overwriting on it.  So, it has GET() attribute only.

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.