C# Property's

Print Friendly, PDF & Email

In C#, properties are nothing but a natural extension of data fields. They are usually known as ‘smart fields’ in C# community. We know that data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language. In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members. 

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

  • Declare fields/variables as Private
  • Provide Public Get and Set methods, through Properties, to access and update the value of a Private field

There are a couple of wats in which you can do this, below there are two read write property’s and two read only property’s. The second example of each is short hand version.

class Attributes
    {
        double _Attr1 = 0;
        public double Attr1 //Read Write
        {
            get
            {
                return this._Attr1;
            }
            set
            {
                this._Attr1 = value;
            }
        }
        public double Attr2 { get; set; } //Read Write
        public double Attr3 { get; } = 33; //ReadOnly
        public double Attr4 => 22; //ReadOnly
    }

Additional access modifiers can be added to change the behavior of the property’s.

Private Access Modifier

Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created, both of these examples are identical, if there is not access modifier defined the the property is assumed to be Private:

private double _Attribute1 = 0;
double _Attribute5 = 0;

Public Access Modifier

Objects that implement public access modifiers are accessible from everywhere in our project. Therefore, there are no accessibility restrictions:

public double Attribute1 = 0;

Protected Access Modifier

The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class. We will talk in more detail about inheritance in module 2 about object-oriented programming. But for now, we are going to take a look at this example to understand the behavior of the protected members:

protected double Attribute1 = 0;

Internal Access Modifier

The internal keyword specifies that the object is accessible only inside its own assembly but not in other assemblies:

internal Attribute1 = 0;

Protected Internal Access Modifier

The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies (projects):

protected internal Attribute1 = 0;

Private Protected Access Modifier

The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error.

private protected Attribute1 = 0;

Leave a Reply

Your email address will not be published. Required fields are marked *