首页 > Windows开发 > 详细

C# 类内部添加索引器

时间:2017-10-31 13:33:40      阅读:251      评论:0      收藏:0      [点我收藏+]

 

    public class PersonTable : Indexer
    {
        public int xuhao { get; set; }
        public string name { get; set; }
        public string tel { get; set; }

        string Indexer.this[int index]
        {
            get
            {
                if (index == 0) return xuhao.ToString();
                else if (index == 1) return name;
                else if (index == 2) return tel;
                else return null;
            }
            set
            {
                if (index == 0) xuhao = Convert.ToInt32(value);
                else if (index == 1) name = value;
                else if (index == 2) tel = value;
            }
        }

        int Indexer.this[string propname]
        {
            get
            {
                if (propname == "xuhao") return 0;
                else if (propname == "name") return 1;
                else if (propname == "tel") return 2;
                else return -1;
            }
        }
    }
    public interface Indexer
    {
        //定义索引器
        string this[int index] { get; set; }
        //获取索引,传入值必须为tolower
        int this[string propname] { get; }
    }

 

C# 类内部添加索引器

原文:http://www.cnblogs.com/GoCircle/p/7761013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!