.net索引器

来源:互联网 发布:客户端编程 编辑:程序博客网 时间:2024/04/30 08:03

1、创建索引

  private static string[] countries =  new string[] { "china", "chile", "uk" };        public string this[int index] { get { return countries[index]; } }        public string[] this[string name]        {            get            {                if ((countries == null) || (countries.Length <= 0)) return null;                return Array.FindAll<string>(countries, delegate(string candicate)                {                    return candicate.StartsWith(name);                });            }        }

2、返回索引值

            SingleColumnCollection c = new SingleColumnCollection();            Console.WriteLine( c[0]);//返回索引值            Console.WriteLine( c["ch"].Length);//返回索引集合长度             Console.WriteLine(  c["chi"][0]);//返回索引集合的第一个值

3、运用委托传递进行索引过滤

 public class Dashboard    {       float[] temps = new float[10]{           26.2F,33.5F,35.6F,39.5F,65.2F,83.5F,24.6F,86.7F,90.3F,100.5F       };       public float this[Predicate<float> predicate]       {           get           {               float[] matches = Array.FindAll<float>(temps, predicate);               return matches[0];           }       }    }

3.1调用

 Dashboard dashboard = new Dashboard();            float actual = dashboard[delegate(float data)            {                return data > 63F;            }];//返回大于63F的第一值            Console.WriteLine(actual);


0 0