Searching...

Translate

Understanding Properties and Indexers

*   2.5 Understanding Properties and Indexers

   Indexers provide the mechanism by which an object can be indexed like an array. Properties offer a streamlined way to manage access to a class’ instance data. They relate to each other because both rely upon another feature of C#: the accessor.

*      2.5.1 Get Accessor, Set Accessor

   One of the goals of object oriented programming to not allow any direct access to any data members. So we have to create a method to access to any data members in corresponding class where data members are declared.

   Get and set accessor is the simply a method defined by user to set the values of data member and return the data member to the method in which it was calling. Here an example of get accessor and set accessor method.


Þ   Example
using System;

class test
{
private int x;
   public int getdata()
   {
         return x;
   }
        public void setdata(int n)
        {
            x = n;
        }

    }
    class Program
    {
        public static void Main(string[] args)
        {
            test t = new test();
            t.setdata(13);
            int temp = t.getdata();
            Console.WriteLine("The No. is = {0}", temp);
            Console.ReadLine();

        }
    }
   Output
The No. is = 13
Þ   Disadvantages

1.     Programmers have to write coding manually.

2.     Programmers have to remember that the data methods are accessed using accessor methods only.

*      2.5.2 Indexers(One Dimension) and property

v Indexers(One Dimension)

   As you know, array indexing is performed using the [ ] operator instead, you create an indexer.

   An indexer allows an object to be indexed like an array. The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints. Syntax for creating one dimensional indexer is described below.

Syntax :-

element-type this[int index]
{
// The get accessor
get
{
// return the value specifi ed by index
}
// The set accessor
set
{
// set the value specifi ed by index
}
}


   Here, element-type is the element type of the indexer. Thus, each element accessed by the indexer will be of type element-type. This type corresponds to the element type of an array.

   The parameter index receives the index of the element being accessed. Inside the body of the indexer two accessors are defined that are called get and set. An accessor is similar to a method except that it does not declare a return type or parameters.

   The accessors are automatically called when the indexer is used, and both accessors receive index as a parameter. If the indexer is on the left side of an assignment statement, then the set accessor is called and the element specified by index must be set.

   Otherwise, the get accessor is called and the value associated with index must be returned. The set method also receives an implicit parameter called value, which contains the value being assigned to the specified index.

   One of the benefits of an indexer is that you can control precisely how an array is accessed, heading off improper access. Here is an example.


Þ   Example
using System;

class myclass
{
private string[] subjects = new string[5];
      public string this[int index]//indexer
      {
          get
          {
             return subjects[index];
          }
          set
          {
             subjects[index] = value;
          }
      }
}
class Program
{
     static void Main(string[] args)
     {
         myclass m = new myclass();
         m[0] = "IAP";
         m[1] = "JAVA";
         m[2] = "CN";
         m[3] = "MP";
         m[4] = "PROJ";
         Console.WriteLine("The array is as below");
         for(int i=0;i<5;i++)
         {
            Console.WriteLine("The Value Of m[{0}] is =
 {1}",i,m[i]);
         }
         Console.ReadLine();
     }
 }
   Output

The array is as below
The Value Of m[0] is = IAP
The Value Of m[1] is = JAVA
The Value Of m[2] is = CN
The Value Of m[3] is = MP    
The Value Of m[4] is = PROJ



   It is not necessary for an indexer to support both get and set. You can create a read-only indexer by implementing only the get accessor. You can create a write-only indexer by implementing only set. 

0 comments:

Post a Comment

Subscribe us