Multiparameter Indexer in C#

该博客展示了用C#实现电话目录多参数索引器的代码。定义了PhoneDir类,包含不同类型的索引器,可根据位置、姓名、电话号码等进行读写操作。还给出了使用示例及输出结果,展示了索引器在不同场景下的调用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

By Ramkumar Durgam

// ' PhoneDirIndex.cs - PhoneDirectory to simulate the use of an Multiparameter Indexer using C#
// -----------------------------------------------------------------------------------------------------
//
using System;

namespace PhoneDirIndexer
{
 class PhoneDir
 {
  private string[] _name;
  private uint[] _phoneNumber;
  private ushort _size;

  public PhoneDir(ushort size)    // Default Constructor
  {
   _size = size;
   _name = new string[size];
   _phoneNumber = new uint[size];

   for (int i=0; i < size; i++)
   {
    _name[i] ="No Name";
    _phoneNumber[i] = 0;
   }
  }


  // Indexer 1.0 - Read/Write Indexer (returns the Name and Phonenumber based on the Position and sets the Name)
  public string this[int pos]
  {
   get 
   { 
    Console.Write("Calling Get on Indexer 1.0...");
    return (_name[pos] + "(" + _phoneNumber[pos] +")") ; 
   }

   set 
   { 
    Console.WriteLine("Calling Set on Indexer 1.0...");
    _name[pos] = value; 
   }
  }


  // Indexer 2.0 - Read/Write Indexer (returns the Name and Phonenumber based on the Position and sets the Name)
  public string this[string name]
  {
   get 
   { 
    Console.Write("Calling Get on Indexer 2.0...");
    int pos;
    pos = -1;
    for (int i=0;i < _size;i++)
    {
     if(_name[i] == name)
     {
      pos = i;
      break;
     }
    }
    if (pos >=0)
     return (_name[pos] + "(" + _phoneNumber[pos] +")") ; 
    else
     return "";
   }
   set 
   { 
    Console.WriteLine("Calling Set on Indexer 2.0...");
    int pos;
    pos = -1;
    for (int i=0;i < _size;i++)
    {
     if(_name[i] == name)
     {
      pos = i;
      break;
     }
    }
    if (pos >=0) 
     _phoneNumber[pos] = Convert.ToUInt16(value); 
   }
  }


  // Indexer 3.0 - Read/Write Indexer to return the name for a given phone number and updates a phonenumber for a given phone number
  public string this[uint phone]
  {
   get    // Returns the name for a given phone number
   {
    Console.Write("Calling Get on Indexer 3.0...");
    int pos = -1;
    for (int i =0; i <  _size; i++)
    {
     if (_phoneNumber[i] == phone)
     {
      pos = i;
      break;
     }
    }
    if (pos >=0 )
     return ( _name[pos] );
    else
     return "";
   }
   set   // Set (updates) the phone number(s) for a given Phone number
   {
    Console.WriteLine("Calling Set on Indexer 3.0...");
    for (int i =0;i < _size; i++)
    {
     if (_phoneNumber[i] == phone)
      _phoneNumber[i] = Convert.ToUInt16(value);
    }
   }
  }


  // Indexer 4.0 - Readonly Indexer (returns the Position for a given Name and Phonenumber combination)
  public int this[string name,uint phone]
  {
   get 
   {
    Console.Write("Calling ReadOnly Indexer 4.0...");
    int pos = -1;

    for (int i=0; i < _size; i++)
    {
     if (_name[i] == name && _phoneNumber[i] == phone)
     {
      pos = i;
      break;
     }
    }
    return pos;
   }
  }

  
 }
 
 class PhoneDirUI
 {
  static void Main(string[] args)
  {
   int noOfListings = 5;
   PhoneDir listing = new PhoneDir(5)  ;

   listing[1] = "John.A";      // OUTPUT: "Calling Set on Indexer 1.0..."
   listing[3] = "John.B";      // OUTPUT: "Calling Set on Indexer 1.0..."

   listing["John.A"] = "1111";    // OUTPUT: "Calling Set on Indexer 2.0..."
   
   listing[1111u] = "1234";     // OUTPUT: "Calling Set on Indexer 3.0..." 
                // Note: 'u' (next to the number) is the only one that causes Indexer3.0 to be called
                //         Without that Indexer 1.0 would have been called becuase it thinks its an integer and not a uint
   
   listing["John.B"] = "2222";    // OUTPUT: "Calling Set on Indexer 2.0..."


   for (int i=0;i < noOfListings; i++)
   {
    Console.WriteLine("listing[{0}] : {1}",i,listing[i]);  //  OUTPUT: "Calling Get on Indexer 1.0... listing[i] : _name(_phone)
   }

   Console.WriteLine("Person Located in Phone number 1111..." + listing[1111u]);  
       //OUTPUT: Calling Get on Indexer 3.0 ... Person Located in Phone Number 1111... 

   Console.WriteLine("Person Located in Phone number 1234..." + listing[1234u]);  
      //OUTPUT: Calling Get on Indexer 3.0 ... Person Located in Phone Number 1234....John.A  (because we updated the phone number above)

   Console.WriteLine("John.B,2222 is located in..." + listing["John.B",2222]);      
     //OUTPUT: Calling ReadOnly Indexer 4.0 ... John.B,2222 is located in...3

   Console.ReadLine();
  }
 }
}

---------------------
Output
--------------------

Calling Set on Indexer 1.0...
Calling Set on Indexer 1.0...
Calling Set on Indexer 2.0...
Calling Set on Indexer 3.0...
Calling Set on Indexer 2.0...
Calling Get on Indexer 1.0...listing[0] : No Name(0)
Calling Get on Indexer 1.0...listing[1] : John.A(1234)
Calling Get on Indexer 1.0...listing[2] : No Name(0)
Calling Get on Indexer 1.0...listing[3] : John.B(2222)
Calling Get on Indexer 1.0...listing[4] : No Name(0)
Calling Get on Indexer 3.0...Person Located in Phone number 1111...
Calling Get on Indexer 3.0...Person Located in Phone number 1234...John.A
Calling ReadOnly Indexer 4.0...John.B,2222 is located in...3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值