by Dinesh Thakur Category: Advanced C#

Property is a feature to add more smartness to data fields for get and set the value.

Properties are named members of classes, structs and interfaces. They provide a flexible mechanism to read, write, or compute the values of private fields through accessors. Properties can be thought of as virtual fields. From the outside, a class' property looks just like a field. But from the inside, the property is generated using the actual class fields.

Property declarations take just those modifiers taken by methods. Unlike languages like Java,C# provides dedicated support for accession and mutation of these properties. Suppose, for instance, that a type contains an internal field called 'age'. With the following code one could specify a property Age, providing accessors and mutators to this internal field

 

public int Age

{

get

{

return this.age;

}

set

{

this.age = value;

}

}

Notice that the term 'value' is used in the above .piece of code. This variable always holds

the value passed to the 'set' block.· For instance, the execution of the following line of ode (assuming the appropriate class instance) would automatically set 'value' in the 'set' block to 4.

person.Age = 4;

This property Age can be described as 'read-write' since it can be both read from and written to. To make a property 'write-only' one simply does not specify a 'get' block; to make it 'readonly' one does not specify a 'set' block. The following piece of code demonstrates the read-only property 'Adult':

 

public bool Adult

{

get

{

if (this.age<18)

       return false;

else

return true;

}

}

A property declared using the static modifiers is classified as a static property; otherwise, it is classified as an instance property. Like other static members, a static property is not associated with a specific instance and cannot be referenced through an instance. Instead, it is associated with the type and can only be referenced an instance. Instead, it is associated with the type and can only be referenced through the type name. For example, in the following statements:

 

Button okButton = new Button();

// using an instance property

string s = okButton.caption;

the caption property is associated with the instance okButton. If caption is as a static property, the class name (Button) must be used instead :

// using static property

strings = Button. Caption;

An instance of a class can be accessed using this in the accessors of an instance property, but it is an error to use this in the accessors of a static property. It is an error to use a virtual, abstract, or override modifier on an accessor of a static property.

The following example demonstrates instances, static and read-only properties. It accepts the name of the employee from the keyboard, increments number of Emp by 1, and displays the Employee name and number.

 

Use of property.

 

using system;

public class Employee

{

public static int numberofEmp;

private static int counter;

private string name;

public string Name

{

   get

         {

   return name;

         }

   set

       {

         name = value;

}

}

public static int Counter

{

get

{

return counter;

}

set

{

counter = value;

}

}

public Employee()

{

Counter = ++ Counter + numberofEmp;

}

}

public class MainClass

{

public static void Main()

{

Employee.numberofEmp = 100;

Employee e1 = new Employee();

e1.Name = "deepika";

console.writeline ("Employee number: {O}", Employee.counter);

console.writeline ("Employee name: {O}", e1.Name);

}

}

 

OUTPUT:

 

Employee number: 101

Employee name: Deepika

Accessors

The accessor of a property contains the executable statements associated with getting (reading or computing) or setting (writing) the property. The accessor declarations take the following forms:

 

set {accessor body}

get {accessor body}

where, accessor body is a block that contains the statements to be executed when the accessor is invoked.

 

The get Accessor

 

The body of the get accessor is similar to that of a method. It must return a value of the property type. The execution of the get accessor is.equivalent to reading the value of the field.

The following is a get accessor that returns the value of private field name :

 

private string name; II the name field

public string Name II the Name property

{

get

{

return name;

}

}

When you reference the property, except as the target of an assignment, the .get accessor is invoked to read the value of the property. For example:

Emp1oyee el = new Employee ();

-

-

console.write (el.Name); II The get accessor is invoked here

The get accessor must terminate is a return or throw statement and control can not flow off the accessor body.

The set Accessor

The set accessor is similar to a method that returns void. It uses an implicit parameter called value, whose type is the type of the property. In the following example, a set accessor is added the Name property.

public string Name

{

       get

     {

             return name;

       }

       set

     {

             name = value;

       }

}

When you assign a value to the property, the set accessor is invoked with an argument that provides the new value. For example:

 

e1 .Name = "Deepika", // The set accessor is invoked here

It is an error to use the implicit parameter name (value) for a local variable declaration in a set accessor.

A property is classified according to the accessors used as follows:

• A property with a get accessor only is called a read-only property. You can not assign a value to a read-only property.

• A property with a set accessor only is called write-only property. You can not reference a write only property except as a target of an assignment.

• A property with both get and set accessors is a read-write property.

In a property declaration, both the get and set accessor must be declared inside the body of the property.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.