Zeb-ur-Rehman
Zeb-ur-Rehman

Reputation: 1209

Access Modifier VS Properties

The purpose of Access modifiers is to hide data members from unauthorized access. while the purpose of Properties is to expose Access modifiers. Where the purpose of access modifier dies. The following is the example.

public class Employee
{
    private int EmployeeID;
    private string Name;
    private int Salary;

    public int EID { get { return this.EmployeeID; } set { this.EmployeeID = value; } }
    public string EName { get { return this.Name; } set { this.Name = value; } }
    public int ESalary { get { return this.Salary; } set { this.Salary = value; } }
}

static void Main(string[] args)
{
    Employee Employee = new Employee();
    Employee.EName = "Zaheer";
}

Here i can access property EName which indirectly access Name. Any comments nd sorry if the question is silly.

Upvotes: 0

Views: 531

Answers (4)

Servy
Servy

Reputation: 203830

The idea of a private field is not that there is no way, whatsoever, for that value to be modified by something from outside of the type. If that were true they would be entirely useless. The idea of a private field is that direct access to the member itself is prohibited from outside of the type, but a limited degree of indirect access can be allowed through some number of non-private members. Those public members will provide some more limited means of accessing the field than public access does.

In your particular example there really isn't much restriction taking place; the value can be accessed at any time by anyone with an instance, or set any time by anyone with an instance. It's still a tad more protected than exposing the field publicly, for example you can't directly create a reference to the field through a ref parameter on a method, but you're right to say that there is very little protection added. It becomes much more useful when the ability of an external entity to modify the field is limited in some way. Perhaps it can get the value and not set it, perhaps certain values can't be set at all, perhaps it will change itself in ways never specified directly by any external entity when methods are called.

The idea at the end of the day is that the definition of the methods/properties will determine what level of access there is to the underlying fields. That level of access can range from almost complete to almost nothing.

Upvotes: 2

John Willemse
John Willemse

Reputation: 6698

Your example properties are all very simple and could be replaced by automatic properties. Properties are used to control the access to a field, not just to expose the field.

As an example, if you have a property called EmailAddress, you might want to check the value for a proper email address before it is assigned. You can handle that in the setter of the property and reject any invalid values. You cannot control this inside your class when you expose a field directly.

This is one of the obvious uses of a property, but there are other uses and advantages over using fields.

Upvotes: 1

Impworks
Impworks

Reputation: 2818

Properties are actually a syntactic sugar for methods that return and set a value of a field. Therefore, they have several advantages:

  1. Properties can perform additional checks. Like, you can catch attempts to set EID to a negative value or EName to an empty string.
  2. Properties can set several fields at once to keep the inner data of the object consistent.
  3. Properties can be used in interfaces, while fields cannot.
  4. Properties can have separate access modifiers for getters and setters.

However, setting a field is more efficient because it does not have the overhead of invoking a method.

Upvotes: 1

Avram Tudor
Avram Tudor

Reputation: 1526

You got it a bit wrong. Properties are data members and access modifiers are the ones that modify the access to the data members. Properties are accessible from outside because they have the 'public' access modifier.

Upvotes: 2

Related Questions