神刀安全网

C# Indexer

In this article, we will discuss:

  • What is an Indexer?
  • Declaring an Indexer
  • The Indexer set Accessor
  • The Indexer get Accessor
  • Example
  • Indexer Overloading
  • Access Modifiers on Accessors
  • Summary

What is an Indexer?

An indexer is a pair of get and set accessors, similar to those of properties.

  • Like a property, an indexer does not allocate memory for storage.
  • Both indexers and properties are used primarily for giving access to other data members with which they are associated and for which they provide get and set access.
    − A property usually represents a single data member.
    − An indexer usually represents multiple data members.
  • Like a property, an indexer can have either one or both of the accessors.
  • Indexers are always instance members; hence, an indexer cannot be declared static.
  • Like properties, the code implementing the get and set accessors does not have to be associated with any fields or properties. The code can do anything, or nothing, as long as the get accessor returns some value of the specified type.

Declaring an Indexer

An indexer does not have a name. In place of the name is the keyword this.

• The parameter list is between square brackets.

• There must be at least one parameter declaration in the parameter list.

Syntax:

ReturnTypethis [ Typeparam1, ... ] {                 get {...}   set {...} } 

C# Indexer

The Indexer set Accessor:

When the indexer is the target of an assignment, the set accessor is called and receives two items of data:

• An implicit parameter, named value, which holds the data to be stored

• One or more index parameters that represent where it should be stored

emp[0] = “Steve”;

Your code in the set accessor must examine the index parameters, determine where the data should be stored, and then store it.

C# Indexer

The left side of the figure shows the actual syntax of the accessor declaration. The right side shows the semantics of the accessor if it were written using the syntax of a normal method.The figure on the right shows that the set accessor has the following semantics:

• It has a void return type.

• It uses the same parameter list as that in the indexer declaration.

• It has an implicit value parameter named value, of the same type as the indexer.

The Indexer get Accessor:

When the indexer is used to retrieve a value, the get accessor is called with one or more index parameters. The index parameters represent which value to retrieve.string s = emp[0];

The code in the get accessor body must examine the index parameters, determine which field they represent, and return the value of that field.

C# Indexer

The left side of the figure shows the actual syntax of the accessor declaration. The right side shows the semantics of the accessor if it were written using the syntax of a normal method. The semantics of the get accessor are as follows:

• It has the same parameter list as in the indexer declaration.

• It returns a value of the same type as the indexer.

Example:

 class Employee     {         public string LastName; // Call this field 0.         public string FirstName; // Call this field 1.         public string CityOfBirth; // Call this field 2.         public string this[int index] // Indexer declaration         {             set // Set accessor declaration             {                 switch (index)                 {                     case 0: LastName = value;                         break;                     case 1: FirstName = value;                         break;                     case 2: CityOfBirth = value;                         break;                     default:                          throw new ArgumentOutOfRangeException("index");                 }             }             get // Get accessor declaration             {                 switch (index)                 {                     case 0: return LastName;                     case 1: return FirstName;                     case 2: return CityOfBirth;                     default:                          throw new ArgumentOutOfRangeException("index");                 }             }         }     }                           

In the above example:

  • The indexer must read and write values of type string—so string must be declared as the indexer’s type. It must be declared public so that it can be accessed from outside the class.
  • The three fields in the example have been arbitrarily indexed as integers 0 through 2, so the formal parameter between the square brackets, named index in this case, must be of type int.
  • In the body of the set accessor, the code determines which field the index refers to and assigns the value of implicit variable value to it. In the body of the get accessor, the code determines which field the index refers to and returns that field’s value.

Indexer Overloading:

A class can have any number of indexers, as long as the parameter lists are different. it isn’t sufficient for the indexer type to be different. This is called indexer overloading, because all the indexers have the same “name”—the this access reference.

Example:

  class MyClass { public string this [ int index ] { get { ... } set { ... } } public string this [ int index1, int index2 ] { get { ... } set { ... } } public int this [ float index1 ] { get { ... } set { ... } } ...   }  

Access Modifiers on Accessors:

  • An accessor can have an access modifier only if the member (property or indexer) has both a get accessor and a set accessor.
  • Although both accessors must be present, only one of them can have an access modifier.
  • The access modifier of the accessor must be strictly more restrictive than the access level of the member.

Example:

  class Person     {         public string Name { get; private set; }         public Person(string name)         {             Name = name;         }     }     class Program     {         static public void Main()         {             Person p = new Person("Steve Smith");             Console.WriteLine("Person's name is {0}", p.Name);         }     }  

Output:

Person’s name is Steve Smith

Summary

In this article, we will discuss:

  • What is an Indexer?
  • Declaring an Indexer
  • The Indexer set Accessor
  • The Indexer get Accessor
  • Example
  • Indexer Overloading
  • Access Modifiers on Accessors

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# Indexer

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址