C#中怎么做到这样的stylevar["charset"]下标为字符串的数组?

C#中怎么做到这样的stylevar["charset"]下标为字符串的数组?
本文来自: 站长(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/721/268745.htm

 

问:
在PHP中数组下标可以是字符串,比如:stylevar["charset"]
这样的好处是非常便于记忆,不容易出错。请问在C#中怎么用这样的数据表示方法,谢谢!
______________________________________________________________________________________________
答1:
用索引器
______________________________________________________________________________________________
答2:
namespace Programming_CSharp
{
   using System;
 
   // a simplified ListBox control
   public class ListBoxTest
   {
      // initialize the list box with strings
      public ListBoxTest(params string[] initialStrings)
      {
         // allocate space for the strings
         strings = new String[256];

         // copy the strings passed in to the constructor
         foreach (string s in initialStrings)
         {
            strings[ctr++] = s;
         }
      }

      // add a single string to the end of the list box
      public void Add(string theString)
      {
         strings[ctr] = theString;
         ctr++;
      }

      // allow array-like access
      public string this[int index]
      {
         get
         {
            if (index < 0 || index >= strings.Length)
            {
               // handle bad index
            }
            return strings[index];
         }
         set
         {
            strings[index] = value;
         }
      }
  
      private int findString(string searchString)
      {
         for (int i = 0;i<strings.Length;i++)
         {
            if (strings[i].StartsWith(searchString))
            {
               return i;
            }
         }
         return -1;
      }
      // index on string
      public string this[string index]
      {
         get
         {
            if (index.Length == 0)
            {
               // handle bad index
            }

            return this[findString(index)];
         }
         set
         {
            strings[findString(index)] = value;
         }
      }


      // publish how many strings you hold
      public int GetNumEntries()
      {
         return ctr;
      }

      private string[] strings;
      private int ctr = 0;
   }

   public class Tester
   {       
      static void Main()
      {
         // create a new list box and initialize
         ListBoxTest lbt =
            new ListBoxTest("Hello", "World");

         // add a few strings
         lbt.Add("Who");
         lbt.Add("Is");
         lbt.Add("John");
         lbt.Add("Galt");

         // test the access
         string subst = "Universe";
         lbt[1] = subst;
         lbt["Hel"] = "GoodBye";
         // lbt["xyz"] = "oops";

         // access all the strings
         for (int i = 0;i<lbt.GetNumEntries();i++)
         {
            Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
         }      // end for
      }         // end main
   }            // end tester
}               // end namespace

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值