Loading...
Gadget by Dot Net Spiders.

Saturday, November 27, 2010

Difference between Properties and Indexers : Properties Vs Indexers

Properties and indexers are two of the most important concepts in C#.Properties are popularly known as 'Smart fields' and they provide access to the fields of a class whereas indexers are popularly known as 'Smart arrays' and they allows objects to be indexed as an array which is useful for looping or iterating or data binding operations.Defining an indexer is much like defining property but there exists a subtle differences between these two.

What is Property?

Properties are natural extension of data fields.The main advantage of using a Property over a public field is, with the property, we can perform some validation or any other logic in the Get and Set accessors before returning any values or assigning to the private field.Another advantage of property is that the data fields are not directly accessible out side the class.We must use the set/get methods to access the data fields.
The general form of declaring a property is as follows. The first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor.


{
get
{
   // Get codes goes here

}
set
{
   // Set codes goes here

}}

Where access_modifier can be private, public, protected or internal. The return_type can be any valid C# type.
The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property with both accessors is read-write.
public class Today 
{ 
 private int day = 31; //"backing store" 
 public int Day
 { 
  get 
  { 
   return day;
   } 
  set
  {
   if ((value > 0) && (value < 32)) 
   { 
    day = value; 
   } 
  } 
 }
} 
In this example, Day is declared as a property so that the set accessor can make sure the Day value is set between 1 and 31.The body of the get accessor is similar to that of a method. It must return a value of the property type. The set accessor is similar to a method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property. When you reference the property, the get accessor is invoked to read the value of the property. It is a bad programming style to change the state of the object by using the get accessor.The get accessor can either be used to return the field value or to compute it and return it.When you assign a value to the property, the set accessor is invoked with an argument that provides the new value.
Today t1 = new Today();
t1.Day = 25;  // the set accessor is invoked here                

System.Console.Write(t1.Day);   // the get accessor is invoked here
Properties can be marked as public, private, protected, internal, or protected internal. The get and set accessors for the same property may have different access modifiers. A property may be declared as a static property using the static keyword. This makes the property available to callers at any time, even if no instance of the class exists. A property may be marked as a virtual property using the virtual keyword. This allows derived classes to override the property behavior using the override keyword.A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual. Lastly, a property can be declared abstract, meaning there is no implementation in the class, and derived classes must write their own implementation.Moreover,it is an error to use a virtual , abstract , or override modifier on an accessor of a static property. 


Drawbacks: 
  1.  A property cannot be passed as an out or ref parameter to a method; a field can.
  2. A property method may take a long time to execute than a datafield. 
What is Indexer ? 

Properties allow you to access objects state with field-like syntax. An indexer is similar to a property.As with properties, we use get and set when defining an indexer. Unlike properties, you are not obtaining a specific data member; rather, you are obtaining a value from the object itself. When you define a property, you define a property name.With indexers, instead of creating a name as you do with properties, you use the this keyword, which refers to the object instance and thus the object name is used. The general form of declaring an indexer is follows. this[argument list]

{
  get
{
// Get codes goes here
}
set
{
 // Set codes goes here
 }
 }
Where the modifier can be private, public, protected or internal .The return type can be any valid C# types. The 'this' is a special keyword in C# to indicate the object of the current class. The formal-argument-list specifies the parameters of the indexer.The indexers must have at least one parameter. Other wise the compiler will generate a compilation error.Indexers cannot be static. This is because static methods do not have access to ‘this’.
class Customer
{
 private string[] name = new string[10]; 
 public string this[int index] 
 { 
    get
  {
       return name[index]; 
     } 
     set 
    { 
       name[index] = value; 
     } 
 } 
} 

class IndexTest
{
  public static void Main() 
  { 
     Customer cust = new Customer(); 
     cust[0] = "Schumaker"; 
     cust[1] = "levin"; 
     cust[2] = "thomson"; 
     Console.WriteLine("{0}\n,{1}\n,{2}\n,", cust[0], cust[1], cust[2]);
     Console.ReadLine();
  }
} 
Differences between properties and indexers  

  1. Properties are accessed by names but indexers are accessed using indexes. 
  2. Properties can be static but Indexers should always be instance members of the class.we cannot have static indexers.
  3. The get accessor of a property does not accept any parameter but the get accessor of the indexer accepts the same formal parameters as the indexer.
  4. The set accessor of a property contains an implicit parameter called "value". The set accessor of an indexer contains the same formal parameters as that of the indexer and also the "value" as an implicit parameter.

Property

Indexer

Allows methods to be called as if they were public data members.

Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.

Accessed through a simple name.

Accessed through an index.

Can be a static or an instance member.

Must be an instance member.

A get accessor of a property has no parameters.

A get accessor of an indexer has the same formal parameter list as the indexer.

A set accessor of a property contains the implicit value parameter.

A set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter.

Supports shortened syntax with Auto-Implemented Properties (C# Programming Guide).

Does not support shortened syntax.


0 comments:

Post a Comment

RECENT POSTS

Related Posts with Thumbnails