Indexer

索引的声明包括:可选的访问修饰符,返回值的类型,关键字this(不能省略),
和函数参数类似的参数(不过是方括号,而不是函数的圆括号),然后是索引体。
你不能使用静态索引,所以你不能在索引的声明中使用static关键字。
索引可以被声明为虚拟的,因此它可以在它的派生类中被重载。
索引的参数不能使用ref/out型参数。例如:
1 None.gif   struct  StringSection
2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
3InBlock.gifpublic char this [ref int at]//错误
4InBlock.gif dot.gif
5ExpandedBlockEnd.gif}

注意,这里的属性名是this,意思是回引类的当前实例,参数列表包含在方括号而非括号之内。

索引的声明和属性一样:只能含有set/get语句。
    当使用一个索引表达式进行读操作时,索引的get语句自动运行
    当使用一个索引表达式进行写操作时,索引的set语句自动运行

属性和索引器

属性和索引器之间有好些差别:

类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。

属性可以是static(静态的)而索引器则必须是实例成员。

为索引器定义的访问函数可以访问传递给索引器的参数,而属性访问函数则没有参数

接口与索引

接口定义索引器,如IList和 IDictionary集合接口都声明了索引器以便访问其存储的项目。

在为接口声明索引器的时候,记住声明只是表示索引器的存在。你只需要提供恰当的访问函数即可,不必包括范围修饰符。只需定义作因名称,相应的实现方法则应该为该类提供该类的索引器的方法.以下代码把索引器声明为接口IImplement的一部分:

ContractedBlock.gif ExpandedBlockStart.gif
1ExpandedBlockStart.gifContractedBlock.gifinterface IImplement dot.gif
2InBlock.gif
3InBlock.gifstring this[int index] 
4ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif
5InBlock.gifget
6InBlock.gifset
7ExpandedSubBlockEnd.gif}
  
8ExpandedBlockEnd.gif}

下面附上一个例子
None.gif using  System;
None.gif
using  System.Collections;
None.gif
// using System.Collections.Generic;
None.gif
using  System.Text;
None.gif
namespace  ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Indexer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            test t 
= new test();
InBlock.gif            
// 初始取值
InBlock.gif
            Console.WriteLine(t[0]);
InBlock.gif            Console.WriteLine(t[
1]);
InBlock.gif            Console.WriteLine(t[
2]);
InBlock.gif
InBlock.gif            
// 重新赋值
InBlock.gif
            t[0= "zhoumin";
InBlock.gif            t[
1= "welcome";
InBlock.gif            Console.WriteLine(t[
0]);
InBlock.gif            Console.WriteLine(t[
1]);
InBlock.gif            Console.WriteLine(t[
2]);
InBlock.gif
InBlock.gif            Console.WriteLine(
"**************************************************");
InBlock.gif
InBlock.gif            t[
"first"= "第一个";
InBlock.gif            t[
"second"= "第2个";
InBlock.gif            t[
"third"= "第3个";
InBlock.gif
InBlock.gif            
// 注意:此处以string类型作为输入参数的Indexer被重新赋值
InBlock.gif
            t["a"= "ha,ha";
InBlock.gif            Console.WriteLine(t[
"first"]);
InBlock.gif            Console.WriteLine(t[
"second"]);
InBlock.gif            Console.WriteLine(t[
"third"]);
InBlock.gif            Console.WriteLine(t[
"what"]);
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 定义私有域 外部不能访问 string数组
InBlock.gif
        private string[] names;
InBlock.gif        
public test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            names 
= new string[3];
InBlock.gif            names[
0= "abc";
InBlock.gif            names[
1= "xyz";
InBlock.gif            names[
2= "911";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.names[index];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.names[index] = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif         
public string this[string n]
InBlock.gif
//        public object this[string n]
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             
/**//*如你你所看到的那样,属性声明倒更像是域声明,只不过它还声明了两个特殊的成员,
InBlock.gif             按照微软的说法就是所谓的访问函数(accessor)。当某一表达式的右边调用属性或者属性用作其他子程序(或者函数)的参数时即会调用get访问函数。
InBlock.gif             反之,当表达式左边调用属性并且通过隐式传递value参数设置私有域值的情况下就会调用set访问函数。
ExpandedSubBlockEnd.gif             你可以创建只读属性,方法是省略set访问函数,这样任何设置属性的尝试都会产生编译错误。
*/
 
InBlock.gif
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ((string)n == "first"return names[0];
InBlock.gif                
if ((string)n == "second"return names[1];
InBlock.gif                
if ((string)n == "third"return names[2];
InBlock.gif                
return "null";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (n == "first") names[0= (string)value;
InBlock.gif                
else if (n == "second") names[1= (string)value;
InBlock.gif                
else if (n == "third") names[2= (string)value;
InBlock.gif                
else names[0= names[1= names[2= (string)value;
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

输出:
abc
xyz
911
zhoumin
welcome
911
****************************************
ha,ha
ha,ha
ha,ha
null

转载于:https://www.cnblogs.com/lanshh/archive/2006/06/26/436108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值