Properties in c# – Free C# Tutorials

In this blog we will discuss about the Properties in c#.

  • One of the design goals of object oriented programming is not to permit any direct access of data members because of the implications of integrity.
  • It is normal to provide special methods known as accessor methods to have access to all data members.
  • We must use only use these methods to set or retrieve values of these members.
  • In the following code we are going to access private data using acessor methods..

code

using System;
class Number
private int number;
public void SetNumber ( int x )   // accessor method
 {
   number = x;
 }
public int GetNumber()   // accessor method
 {
   return number;
 }
}
class NumberTest
 {
  public static void Main()
   {
    Number n = new Number();
    n.SetNumber(50);   // set value
    Console.WriteLine( " Number = " + n.GetNumber() );  // getvalue
    b.number;    //Error! cannot access private data
   }
}

output

Number = 50
  • The SetNumber is also known as mutator method.
  • Though accessor methods work well with all the OOPS languages including C,C++ and java.However it has the following drawbacks:
  1. We have to code the accessor methods manually.
  2. users have to remember that they have to use accessor methods inorder to access the data members.

In order to overcome this fault c# provides a concept known as Properties that has the same capabilities as accessor methods but it is much simpler and neat to use.

  • Using a property a programmer can access a data members as though they are public fields

code:

using System;
Class Number
{
  private int number;
  public int Anumber;  // property
  {
   get
   {
    return number;
   }
   set
   {
    number = value;
   }
  }
}
class PropertyTest
{
  public void static Main()
  {
     Number n= new Number();
     n.Anumber = 100;
     int m = n.Anumber;
     Console.WriteLine( " Number = " +m );
  }
}

output

Number = 100
  • The class now declares a property known as Anumber of type int and defines a method known as get accessor method( getter) and a set accessor method (setter)
  • The getter method used the return keyword to return the fields values to the caller.
  • The setter method uses the keyword value to receive the value being passed in from the user.
  • The type of value is defined by type of property.
  • As the names implies the getter method is used to get the values and the setter method is used to set the values.

n.Anumber = 100;

  • invokes the setter method and places the integer value 100 in a variable named value which is in turn assigned  to the field number.

Similarly the statement

int m = n.Anumber;

  • invokes the getter method and assigns the value of property to m.
  • A property can either  omit a get or set clause.
  • A property that has only getter method is known as read-only property and a property with only setter method in known as write-only property.
  • A write-only property is very rarely used.Some of the other powerful features of properties are listed below:
  1. Other that fetching the value of a variable, a get clause uses code to calculate the value of the property using other fields and returns the results.This means that the property is simply not tied to data members and they can also represent dynamic data.
  2. Like methods properties are also inheritable.We can use modifiers abstract,virtual,new  and override with them appropriately.
  3. The static modifier can be used to declare properties that belongs to the whole class rather than to a specific instance of a class.

This is all about the properties in C# . For any doubts leave in below comments.

Leave a Comment

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