Add an indexer in an interface : Interface « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » InterfaceScreenshots 
Add an indexer in an interface
Add an indexer in an interface

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add an indexer in an interface. 
 
using System; 
 
public interface ISeries 
  // an interface property 
  int next 
    get; // return the next number in series 
    set; // set next number 
  
 
  // an interface indexer 
  int this[int index] { 
    get; // return the specified number in series 
  

 
// Implement ISeries. 
class ByTwos : ISeries 
  int val; 
 
  public ByTwos() { 
    val = 0
  }  
 
  // get or set value using a property 
  public int next 
    get {  
      val += 2
      return val; 
    
    set 
      val = value; 
    
  
 
  // get a value using an index 
  public int this[int index] { 
    get {  
      val = 0
      for(int i=0; i<index; i++
        val += 2
      return val; 
    
  

 
// Demonstrate an interface indexer. 
public class SeriesDemo4 
  public static void Main() { 
    ByTwos ob = new ByTwos()
 
    // access series through a property 
    for(int i=0; i < 5; i++
      Console.WriteLine("Next value is " + ob.next)
 
    Console.WriteLine("\nStarting at 21")
    ob.next = 21
    for(int i=0; i < 5; i++
      Console.WriteLine("Next value is " 
                         ob.next)
 
    Console.WriteLine("\nResetting to 0")
    ob.next = 0
 
    // access series through an indexer 
    for(int i=0; i < 5; i++
      Console.WriteLine("Next value is " + ob[i])
  
}

           
       
Related examples in the same category
1.Interface with method name conflicts
2.Demonstrate the ByTwos interfaceDemonstrate the ByTwos interface
3.Demonstrate the ByTwos interface 2Demonstrate the ByTwos interface 2
4.Use a property in an interfaceUse a property in an interface
5.One interface can inherit anotherOne interface can inherit another
6.Explicitly implement an interface memberExplicitly implement an interface member
7.Use explicit implementation to remove ambiguityUse explicit implementation to remove ambiguity
8.Two class inherit one interfaceTwo class inherit one interface
9.Using interface 3Using interface 3
10.illustrates interfacesillustrates interfaces
11.illustrates implementing multiple interfacesillustrates implementing multiple interfaces
12.illustrates inheriting from a class and implementing multiple interfacesillustrates inheriting from a class and implementing multiple interfaces
13.illustrates casting an object to an interfaceillustrates casting an object to an interface
14.illustrates deriving an interface from one interfaceillustrates deriving an interface from one interface
15.illustrates deriving an interface from multiple interfacesillustrates deriving an interface from multiple interfaces
16.illustrates an explicit interface member implementationillustrates an explicit interface member implementation
17.illustrates interface member hidingillustrates interface member hiding
18.Demonstrates the use of a simple interfaceDemonstrates the use of a simple interface
19.Interface demoInterface demo
20.Interface demo: implements two interfacesInterface demo: implements two interfaces
21.Interface castingInterface casting
22.Overriding InterfacesOverriding Interfaces
23.Overriding Interfaces: Tester Overriding InterfacesAsOverriding Interfaces: Tester Overriding InterfacesAs
24.A safe method of determining whether a class implements a particular interfaceA safe method of determining whether a class implements a particular interface
25.Interfaces:Working with InterfacesInterfaces:Working with Interfaces
26.Reimplementation of Interfaces
27.Interface with event
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.