msdn中string.Chars的声明是这样的
public char Chars[
int index
] { get; }目的是去除string中指定索引处的字符,注意声明的属性名称是Chars而不是this,但为什么能作为默认的索引器呢?自定义类能实现这个效果吗?(当然在vb中可以用default property关键字指定,c#呢?)

解决方案 »

  1.   

    我用Reflector反编译的结果是这样的public char this[int index] { get; }
     
      

  2.   


    那么Chars属性呢 难道是另外一个属性?冗余属性?
      

  3.   

    String類的索引用應該是類似下面這樣定義的        [System.Runtime.CompilerServices.IndexerName("Chars")]
            public char this[int index]
            {
                get 
                { 
                   //內部代碼,返回一個char
                }
            }由於設置了IndexerName屬性為Chars,所以你用ildasm工具打開會發現索引器定義是Chars,如下:.property instance char Chars(int32)
    {
      .get instance char System.String::get_Chars(int32)
    } // end of property String::Chars
    關於這個問題,你手工寫一個類,實現索引器,並設置IndexerName Attribute,然後用ildasm打開就明白了。