📜  C#–使用字符串作为索引的索引器

📅  最后修改于: 2021-05-29 19:22:59             🧑  作者: Mango

先决条件:C#中的属性

索引器允许类或结构的实例像数组一样被索引。通过使用索引器,类的行为将类似于虚拟数组。可以在不显式指定类型或实例成员的情况下设置或检索索引值。索引器类似于属性,不同之处在于它们的访问器采用参数。索引器几乎类似于“属性” 。索引器和属性之间的主要区别在于,索引器的访问器将使用参数。

使用字符串作为索引的索引器:

此方法将为您提供更多信息,可读性。如果我们想使用字符串作为索引来检索信息。

例子:

ic["username"] = "user12";  
ic["password"] = "12345";  

与下面给出的代码相比,这将提供更高的可读性。

ic[0] = "user12";  
ic[1] = "12345";  

句法:

[access_modifier] [return_type] this [argument_list]
{
 get  
 {
    // retrieval code or code to get the value 
 }
 set  
 {
   // code for setting value to member
 }

在以上语法中:

  • access_modifier :它可以是公共的,私有的,受保护的或内部的。
  • return_type :可以是任何有效的C#类型。在这种情况下,它将是字符串类型。
  • this :它是指向当前类的对象的关键字。
  • arguments_list :这指定索引器的参数列表。
  • get {}和set { } :这些是访问器。

例子:

C#
// C# program to illustrate the Indexers 
// Using String as an Index 
using System; 
  
// class declaration 
class IndexerPOC 
{ 
  
    // class members 
    private string[] val = new string[4]; 
    
    // indexer array 
    private string[] indices={"username","password","email","Book"};
  
    // Indexer declaration 
    // Here pubic is the modifier 
    // string is the return type of 
    // Indexer and "this" is the keyword 
      // which refer to the calling object.
    // having parameters list 
    public string this[string index] 
    { 
  
        // get Accessor 
        // retrieving the values 
        // stored in val[] array 
        // of strings using string indexer.
        get
        { 
  
            return val[Array.IndexOf(indices,index)]; 
        } 
  
        // set Accessor 
        // setting the value at 
        // passed i of val 
        set
        { 
  
            // value keyword is used 
            // to define the value 
            // being assigned by the 
            // set indexer. 
            val[ Array.IndexOf(indices,index)] = value; 
        } 
    } 
} 
  
// Driver Class 
class Program { 
      
    // Main Method 
    public static void Main() { 
          
        // creating an object of parent class which 
        // acts as primary address for using Indexer 
        IndexerPOC ic = new IndexerPOC(); 
  
        // Inserting values in ic[] 
        // Here we are using the object 
        // of class as an array 
        ic["username"] = "user12"; 
        ic["password"] = "12345"; 
        ic["email"] = "user123@gmail.com"; 
        ic["Book"]="CSHARP";
        Console.Write("Printing values stored in objects used as arrays\n"); 
          
        // printing values 
        Console.WriteLine("UserName = {0}", ic["username"]); 
        Console.WriteLine("Password = {0}", ic["password"]); 
        Console.WriteLine("Email = {0}", ic["email"]); 
        Console.WriteLine("Book = {0}", ic["Book"]); 
      
    } 
}


输出:

Printing values stored in objects used as arrays
UserName = user12
Password = 12345
Email = user123@gmail.com
Book = CSHARP