Thema: C# Indexer

Einzelnen Beitrag anzeigen

Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#1

C# Indexer

  Alt 23. Sep 2005, 19:58
Hallo,
ich beginne gerade damit mich mit C# zu beschäftigen.
Da man immer klein anfängt habe ich völlig sinnlose Klasse
gebastelt um das mit dem Indexer zu probieren.
Z.Z. stelle ich mir einen Indexer so vor wie eine Array-Property
in Delphi. In Delphi kann eine Array-Propperty einen beliebigen
Bezeichner haben. Unter C# scheint es nur mit this zu
funktionieren. Da das die Sache extrem einschränken würde
(nur eine Array-Proprty pro Klasse) kann ich mir das nicht vorstellen.
Code:
using System;

namespace myarray
{
   /// <summary>
   /// Zusammenfassende Beschreibung für Class.
   /// </summary>
   public class TArrayClass
   {
     private int[] FItems;

     public int Count
      {get{return FItems.Length;}}

     public int this[int Index]{ // <- Wie kann ich hier einen anderen Bezeichner z.B. Items verwenden
      get {return FItems[Index];}
      set {FItems[Index]=value;}
      }

     public TArrayClass(int Capacity)
     {
      FItems = new int[Capacity];
     }
   }
   class Class
   {
      /// <summary>
      /// Der Haupteintrittspunkt für die Anwendung.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
        TArrayClass ac = new TArrayClass(5);
        Console.WriteLine(ac.Count.ToString());
        Console.WriteLine(); // Leerzeile

        for (int iCnt=0;iCnt<ac.Count;iCnt++)
        { ac[iCnt]=iCnt;}

        for (int iCnt=0;iCnt<ac.Count;iCnt++)
         {Console.WriteLine(ac[iCnt].ToString());}
         
        Console.ReadLine();
      }
   }
}
I come from outer space to save the human race
  Mit Zitat antworten Zitat