Penjelasan Sederhana Indexers C# di Asp.net

Indexers memungkinkan contoh class atau struct diindeks seperti array. Nilai yang diindex dapat ditetapkan atau diambil tanpa secara eksplisit menentukan type atau instance member.

Indexers mirip dengan properti kecuali bahwa accessors Indexers mengambil parameter. Contoh berikut mendefinisikan class generik dengan mendapatkan dan set metod accessor sederhana untuk menetapkan dan mengambil nilai.
Class Program menciptakan sebuah instance dari class ini untuk menyimpan string.

using System;

class SampleCollection
{
   // Declare an array to store the data elements.
   private T[] arr = new T[100];

   // Define the indexer to allow client code to use [] notation.
   public T this[int i]
   {
      get { return arr[i]; }
      set { arr[i] = value; }
   }
}

class Program
{
   static void Main()
   {
      var stringCollection = new SampleCollection();
      stringCollection[0] = "Hello, World";
      Console.WriteLine(stringCollection[0]);
   }
}
// The example displays the following output:
//       Hello, World.

  • Expression Body Definitions

Hal ini biasa terjadi pada indexers get atau set accessor untuk terdiri dari satu pernyataan yang return atau set nilai.
Memberbertemakan ekspresi memberikan sintaks yang disederhanakan untuk mendukung skenario ini.
Dimulai dengan C # 6, indexers read-only dapat diimplementasikan sebagai member bertipe ekspresi, seperti ditunjukkan oleh contoh berikut.
using System;

class SampleCollection
{
   // Declare an array to store the data elements.
   private T[] arr = new T[100];
   int nextIndex = 0;
   
   // Define the indexer to allow client code to use [] notation.
   public T this[int i] => arr[i];
   
   public void Add(T value)
   {
      if (nextIndex >= arr.Length) 
         throw new IndexOutOfRangeException($"The collection can hold only {arr.Length} elements.");
      arr[nextIndex++] = value;
   }
}

class Program
{
   static void Main()
   {
      var stringCollection = new SampleCollection();
      stringCollection.Add("Hello, World");
      System.Console.WriteLine(stringCollection[0]);
   }
}
// The example displays the following output:
//       Hello, World.
Perhatikan bahwa => mengenalkan body ekspresi, dan keyword yang digunakan tidak digunakan.
Dimulai dengan C # 7, keduanya return & set accessor bisa diimplementasikan sebagai member body yang ber-ekspresi. Dalam hal ini, kedua get dan set keyword harus digunakan. Sebagai contoh
using System;

class SampleCollection
{
   // Declare an array to store the data elements.
   private T[] arr = new T[100];

   // Define the indexer to allow client code to use [] notation.
   public T this[int i]
   {
      get => arr[i]; 
      set => arr[i] = value; 
   }
}

class Program
{
   static void Main()
   {
      var stringCollection = new SampleCollection();
      stringCollection[0] = "Hello, World.";
      Console.WriteLine(stringCollection[0]);
   }
}
// The example displays the following output:
//       Hello, World.

  • Tentang Indexers

1. Indexers memungkinkan objek Indexers dengan cara yang mirip dengan array.
2. Accessor get mengembalikan nilai. Accessor yang diset memberi nilai.
3. Keyword ini digunakan untuk mendefinisikan Indexers.
4. Keyword value digunakan untuk menentukan nilai yang ditetapkan oleh pengenal set.
5. Indexers tidak harus Indexers  dengan nilai integer; Terserah kita bagaimana menentukan mekanisme pencarian spesifik.
6. Indexers bisa jadi overloaded.
7. Indexers dapat memiliki lebih dari satu parameter formal, misalnya saat mengakses array dua dimensi.
Previous
Next Post »