{ 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 hereProperties 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:
- A property cannot be passed as an out or ref parameter to a method; a field can.
- A property method may take a long time to execute than a datafield.
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.
{ 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
- Properties are accessed by names but indexers are accessed using indexes.
- Properties can be static but Indexers should always be instance members of the class.we cannot have static indexers.
- 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.
- 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. |