ALTE DOCUMENTE
|
|||||||||
This lesson teaches C# Indexers. Our objectives are as follows:
Indexers are real easy. They allow your class to be used just like an array. On the inside of a class, you manage a collection of values any way you 424f55e want. These objects could be a finite set of class members, another array, or some complex data structure. Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers. Here's an example.
using System;
<summary>
/// A simple indexer
example.
</summary>
class IntIndexer
}
public string this[int pos]
set
}
static void Main(string[] args)
]: ", i, myInd[i]);
}
}
}
Listing 11-1 shows how to implement an Indexer. The IntIndexer class has a string array named myData. This is a private array that external users can't see. this array is initialized in the constructor, which accepts an int size parameter, instantiates the myData array, and then fills each element with the word "empty".
The next class member is the Indexer, which is identified by the this keyword and square brackets, this[int pos]. It accepts a single position parameter, pos. As you may have already guessed, the implementation of an Indexer is the same as a Property. It has get and set accessors that are used exactly like those in a Property. This indexer returns a string, as indicated by the string return value in the Indexer declaration.
The
Using an integer is a common means of accessing arrays in many languages, but the C# Indexer goes beyond this. Indexers can be declared with multiple parameters and each parameter can be a different type. Additional parameters are separated by commas, the same as a method parameter list. Valid parameter types for Indexers include integers, enums, and strings. Additionally, Indexers can be overloaded. In listing 11-2, we modify the previous program to accept overloaded Indexers that accept different types.
using System;
<summary>
/// Implements
overloaded indexers.
</summary>
class OvrIndexer
}
public string this[int pos]
set
}
public string this[string data]
}
return count.ToString();
}
set
}
}
}
static void Main(string[] args)
]: ", i, myInd[i]);
}
Console.WriteLine("\nNumber of
\"no value\" entries: ", myInd["no value"]);
}
}
Listing 11-2 shows how to overload Indexers. The first Indexer, with the integer parameter, pos, is the same as in Listing 11-1, but there is a new Indexer that takes a string parameter. The get accessor of the new indexer returns a string representation of the number of items that match the parameter value, data. The set accessor changes each entry in the array that matches the data parameter to the value that is assigned to the Indexer.
The behavior of the overloaded Indexer that takes a
string parameter is demonstrated in the
The reason both Indexers in Listing 11-2 can coexist in the same class is because they have different signatures. An Indexer signature is specified by the number and type of parameters in an Indexers parameter list. The class will be smart enough to figure out which Indexer to invoke, based on the number and type of arguments in the Indexer call. An indexer with multiple parameters would be implemented something like this:
public object this[int param1, ...,
int paramN]
set
In summary, you now know what Indexers are for and how they're used. You can create an Indexer to access class members similar to arrays. Overloaded and multi-parameter Indexers were also covered.
|