Home |  JournalSeek |  SoftwareSeek |  GenomeSeek |  Expression |  Developer |  Research
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

C# Comparative Contents
  1. C# and Java
  2. Properties
  3. Indexers
  4. Delegates
  5. Events
  6. Enums
  7. Collections and the Foreach Statement
  8. Structs
  9. Type Unification
  10. Operator Overloading
  11. Polymorphism
  12. Interfaces
  13. Versioning
  14. Parameter Modifiers
  15. Attributes
  16. Selection Statements
  17. Predefined Types
  18. Field Modifiers
  19. Jump Statements
  20. Assemblies, Namespaces & Access Levels
  21. Pointer Arithmetic
  22. Rectangular Arrays
  23. Constructors and Destructors
  24. Managed Execution Environments
  25. Libraries
  26. Interoperability
  27. Conclusion


Side Panel
Privacy Policy About Us Contact Us