本帖最后由 pfworld 于 2009-06-07 12:31:07 编辑

解决方案 »

  1.   

      public class Topic
            {
                private string title01;            public string Title01
                {
                    get { return title01; }
                    set { title01 = value; }
                }            private string title02;            public string Title02
                {
                    get { return title02; }
                    set { title02 = value; }
                }            private string title03;            public string Title03
                {
                    get { return title03; }
                    set { title03 = value; }
                }            public string this[int index]
                {
                    get
                    {
                        switch (index)
                        {
                            case 0:
                                return title01;
                            case 1:
                                return title02;
                            case 2:
                                return title03;
                            default:
                                return "";
                        }
                    }
                    set
                    {
                        switch (index)
                        {
                            case 0:
                                title01 = value;
                                return;
                            case 1:
                                title02 = value;
                                return;
                            case 2:
                                title03 = value;
                                return;
                            default:
                                return;
                        }
                               
                    }
                }
            } 
    这样写 增加索引属性.
      

  2.   

    谢谢!!想起来了!!this索引器!!晕!!
      

  3.   

    对,用this撒,返回不同的类型,试试泛型
      

  4.   

    也可以返回object,在强制类型转换
      

  5.   

    public object this[int index] 
                { 
                    get 
                    { 
                        switch (index) 
                        { 
                            case 0: 
                                return title01; 
                            case 1: 
                                return title02; 
                            case 2: 
                                return title03; 
                            default: 
                                return ""; 
                        } 
                    } 
                    set 
                    { 
                        switch (index) 
                        { 
                            case 0: 
                                title01 = value; 
                                return; 
                            case 1: 
                                title02 = value; 
                                return; 
                            case 2: 
                                title03 = value; 
                                return; 
                            default: 
                                return; 
                        } 
                              
                    } 但是这个要进行数据转换!!有好的方法不?
      

  6.   

    使用 this 索引器。无法返回不同类型的值,有一个替代的方法是,返回 object 类型,引用类型没问题,值类型的话返回前boxing一下。然后在外部去unboxing 或者使用 as 操作符来还原类型。或者,你可以使用索引器重载public int this[int index]
    {
    }public string this[string index]
    {
    }....
      

  7.   

    我现在要返回三种类型  string.double,bool  关键是索引器的参数不要确定,一般int,string,不知道还有什么号方法!!或者this.(int index,string index2,string index3)使用this[1,null,null]??