Home |  JournalSeek |  SoftwareSeek |  GenomeSeek |  Expression |  Developer |  TakeOnIt
Return to Genamics Home

3. Indexers

C# provides indexers allow objects to be treated like arrays, except that like properties, each element is exposed with a get and/or set method.

public class Skyscraper
{
    Story[] stories;
    public Story this [int index] {
         get {
               return stories [index];
         }
         set {
              if (value != null) {
                  stories [index] = value;
              }
         }
    }
    ...
}

Skyscraper empireState = new Skyscraper (...);
empireState [102] = new Story ("The Top One", ...);

4. Delegates

A delegate can be thought of as a type-safe object-oriented function pointer, which is able to hold multiple methods rather than just one. Delegates handle problems which would be solved with function pointers in C++, and interfaces in Java. It improves on the function pointer approach by being type safe and being able to hold multiple methods. It improves on the interface approach by allowing the invocation of a method without the need for inner-class adapters or extra code to handle multiple-method invocations. The most important use of delegates is for event handling, which is in the next section (which gives an example of how delegates are used).


Previous     Next


C# 3.0 in a Nutshell (April 2007) - by Joseph Albahari and Ben Albahari.









Add To Favorites
Email This Page



Side Panel
Privacy Policy About Us Contact Us