Indexers don't have to operate on actual arrays : Indexer « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » IndexerScreenshots 
Indexers don't have to operate on actual arrays
Indexers don't have to operate on actual arrays

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Indexers don't have to operate on actual arrays. 
 
using System; 
 
class PwrOfTwo {  
 
  /* Access a logical array that contains 
     the powers of 2 from 0 to 15. */ 
  public int this[int index] { 
    // Compute and return power of 2. 
    get 
      if((index >= 0&& (index < 16)) return pwr(index)
      else return -1
    
 
    // there is no set accessor 
  
 
  int pwr(int p) { 
    int result = 1
 
    for(int i=0; i<p; i++
      result *= 2
     
    return result; 
  
}  
  
public class UsePwrOfTwo {  
  public static void Main() {  
    PwrOfTwo pwr = new PwrOfTwo()
 
    Console.Write("First 8 powers of 2: ")
    for(int i=0; i < 8; i++
      Console.Write(pwr[i" ")
    Console.WriteLine()
 
    Console.Write("Here are some errors: ")
    Console.Write(pwr[-1" " + pwr[17])
 
    Console.WriteLine()
  
}


           
       
Related examples in the same category
1. Use an indexer to create a fail-soft arrayUse an indexer to create a fail-soft array
2. Overload the FailSoftArray indexerOverload the FailSoftArray indexer
3. A two-dimensional fail-soft arrayA two-dimensional fail-soft array
4. Create a specifiable range array classCreate a specifiable range array class
5. Define indexerDefine indexer
6. Indexer: allow array like indexIndexer: allow array like index
7. illustrates the use of an indexer 1illustrates the use of an indexer 1
8. Implements an indexer in a classImplements an indexer in a class
9. Illustrates the use of an indexer
10. Implements an indexer and demonstrates that an indexer does not have to operate on an arrayImplements an indexer and demonstrates that an indexer does not have to operate on an array
11. C# Properties and Indexers
12. Indexing with an Integer IndexIndexing with an Integer Index
13. Indexing with an String Index
14. Indexing with Multiple ParametersIndexing with Multiple Parameters
w_ww_._ja___v___a___2___s__._c__om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.