Properties in C#

 

Properties:

 

v Properties are named members of classes, structures, and interfaces.

 

v Properties are physical description of an object like name, height and weight etc.

 

v Member variables or methods in a class or structures are called Fields.

 

v Properties never used to store any data, it just acts as an interface or medium to transfer the data.

 

v We use the Properties as they are public data members of a class, but they are actually special methods called accessors.

 

v The Properties in C# are used as a mechanism to set and get the values of data members of a class outside of that class.

 

What are Accessors in C#?

The Assessors are nothing but special methods which are used to set and get the values from the underlying data member (i.e. variable) of a class. Assessors are of two types. They are as follows:

 

1.   Set Accessor.

2.   Get Accessor.

 

 

 

1. Set Accessor:

 

v The set accessor is used to set the data (i.e. value) into a data field i.e. a variable of a class.

 

v This set accessor contains a fixed variable named value.

 

v Whenever we call the property to set the data, whatever data (value) we are supplying will come and store inside the variable called value by default.

 

v Using a set accessor, we cannot get the data.

 

Syntax: set {DataFieldName = value; }

 

2. Get Accessor:

 

v The get accessor is used to get the data from the data field i.e. variable of a class.

 

v Using the get accessor, we can only get the data, we cannot set the data.

 

Syntax: get {return DataFieldName ;}

 

 

 

 

 

 

Following is the syntax of defining a property with get and set accessor in c# programming language.

 

 

 

<access_modifier> <return_type> <property_name>

{

    get

    {

       // return property value

    }

    set

    {

      // set a new value

    }

}

 

If you observe the above syntax, we used an access modifier and return type to define a property along with get and set accessors to make required modifications to the class variables based on our requirements.

 

The C#.NET supports four types of properties. They are as follows

 

1.   Read-Only Property.

2.   Write Only Property.

3.   Read Write Property.

4.   Auto-Implemented Property.

 

 

1. Read-Only Property:

 

v The Read-Only Property is used to read the data from the data field i.e. read the data of a variable.

 

v Using this Read-Only Property, we cannot set the data into the data field.

 

v This property will contain only one accessor i.e. get accessor.

 

Syntax:

AccessModifier Datatype PropertyName

{

      get {return DataFieldName;}

}

 

2. Write Only Property:

 

The Write-Only Property is used to write the data into the data field i.e. write the data to a variable of a class.

 

v Using this Write-Only Property, we cannot read the data from the data field.

 

v This property will contain only one accessor i.e. set accessor.

 

 

 

Syntax:

AccessModifier Datatype PropertyName

{

       set {DataFieldName = value;}

}

 

3. Read Write Property:

 

v The Read-Write Property is used for both reading the data from the data field as well as writing the data into the data field of a class.

 

v This property will contain two accessors i.e. set and get. The set accessor is used to set or write the value to a data field and the get accessor is read the data from a variable.

 

Syntax:

AccessModifier Datatype PropertyName

{

      set {DataFieldName = value;}

      get {return DataFieldName;}

}

 

 

 

 

 

 

 

 

 

4. Auto-Implemented Property:

 

In c#, a property is called an auto-implemented property when it contains accessors (get, set) without having any logic implementation.

 

Generally, the auto-implemented properties are useful whenever there is no logic implementation required in property accessors.

 

Syntax: AccessSpecifier Datatype PropertyName { get; set; }

Example: public int Id { get; set; }

 

Advantages of using Properties in C#?

 

v Properties will provide the abstraction to the data fields.

 

v They also provide security to the data fields.

 

v Properties can also validate the data before storing it in the data fields.

 

Real Life Examples:

 

Ø A car is having multiple parts like wheels, engine, steering, gears, etc. which bind together to form an object that is a car.

 

Ø A Human is having height, weight and colour etc. These all are the properties of human.

 

Comments

Popular posts from this blog

.NET Framework Introducton

do while loop in C#

For loop