VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#索引

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

  访问器尤为重要。有关更多信息,请参见非对称访问器可访问性(C#编程指南)。

  http://www.cnblogs.com/mo

  索引器在语法上方便您创建客户端应用程序可将其作为数组访问的类、结构或接口。索引器经常是在主要用于封装内部集合或数组的类型中实现的。例如,假定具有一个名为TempRecord的类,此类表示在24小时内的10个不同时间记录的华氏度。此类包含一个表示温度的float类型的名为“temps”的数组和表示记录温度的日期的DateTime。通过在此类中实现一个索引器,客户端可以通过floattemp=tr[4]而不是floattemp=tr.temps[4]语法访问TempRecord实例中的温度。索引器表示法不仅简化了客户端应用程序的语法,还使其他开发人员能够更加直观地理解类及其用途。

  要声明类或结构上的索引器,请使用this关键字,如下例所示:

  复制代码

publicintthis[intindex]  //Indexerdeclaration
{
  //getandsetaccessors
}

备注

  索引器类型及其参数类型必须至少如同索引器本身一样是可访问的。有关可访问级别的更多信息,请参见访问修饰符。

  有关如何对接口使用索引器的更多信息,请参见接口索引器。

  索引器的签名由其形参的数量和类型组成。它不包括索引器类型或形参名。如果在同一类中声明一个以上的索引器,则它们必须具有不同的签名。

  索引器值不属于变量;因此,不能将索引器值作为ref或out参数进行传递。

  要为索引器提供一个其他语言可以使用的名字,请使用声明中的name属性。例如:

  复制代码

[System.Runtime.CompilerServices.IndexerName("TheItem")]
publicintthis[intindex] //Indexerdeclaration
{
}

 

  此索引器将具有名称TheItem。不提供名称属性将生成Item默认名称。

  示例1

  说明

  下面的示例说明如何声明私有数组字段、temps和索引器。使用索引器可直接访问实例tempRecord[i]。另一种使用索引器的方法是将数组声明为public成员并直接访问它的成员tempRecord.temps[i]。

  请注意,当计算索引器的访问时(例如,在Console.Write语句中),将调用get访问器。因此,如果get访问器不存在,将发生编译时错误。

  代码

  C#复制代码

classTempRecord
{
  //Arrayoftemperaturevalues
  privatefloat[]temps=newfloat[10]{56.2F,56.7F,56.5F,56.9F,58.8F,
                      61.3F,65.9F,62.1F,59.2F,57.5F};
  //Auto-ImplementedProperty
  System.DateTimedate{get;set;}
  //Toenableclientcodetovalidateinput
  //whenaccessingyourindexer.
  publicintLength
  {
    get{returntemps.Length;}
  }
  //Indexerdeclaration.
  //Inputparameterisvalidatedbyclient
  //codebeforebeingpassedtotheindexer.
  publicfloatthis[intindex]
  {
    get
    {
      returntemps[index];
    }
    set
    {
      temps[index]=value;
    }
  }
}
classMainClass
{
  staticvoidMain()
  {
    TempRecordtempRecord=newTempRecord();
    //Usetheindexer'ssetaccessor
    tempRecord[3]=58.3F;
    tempRecord[5]=60.1F;
    //Usetheindexer'sgetaccessor
    for(inti=0;i<10;i++)
    {
      //Thisexamplevalidatestheinputontheclientside.Youmay
      //choosetovalidateitintheclassthatimplementstheindexer,andthrowan
      //exceptionorreturnanerrorcodeinthecaseofinvalidinput.
      if(i<tempRecord.Length)
      {
        System.Console.WriteLine("Element#{0}={1}",i,tempRecord[i]);
      }
      else
      {
        System.Console.WriteLine("Indexvalueof{0}isoutofrange",i);
      }
    }
    //Uncommentthiscodetoseehowthe.NETFrameworkhandlesindexerexceptions
    //try
    //{
    //  System.Console.WriteLine("Element#{0}={1}",tempRecord[tempRecord.Length]);
    //}
    //catch(System.ArgumentOutOfRangeExceptione)
    //{
    //  System.Console.WriteLine(e);
    //}
  }
}

 

  使用其他值进行索引

  C#并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类索引器。由于访问器可被重载,字符串和整数版本可以共存。

  示例2

  说明

  在此例中,声明了存储星期几的类。声明了一个get访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回0,星期一将返回1,等等。

  代码

  C#复制代码

//Usingastringasanindexervalue
classDayCollection
{
  string[]days={"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
  //Thismethodfindsthedayorreturns-1
  privateintGetDay(stringtestDay)
  {
    inti=0;
    foreach(stringdayindays)
    {
      if(day==testDay)
      {
        returni;
      }
      i++;
    }
    return-1;
  }
  //Thegetaccessorreturnsanintegerforagivenstring
  publicintthis[stringday]
  {
    get
    {
      return(GetDay(day));
    }
  }
}
classProgram
{
  staticvoidMain(string[]args)
  {
    DayCollectionweek=newDayCollection();
    System.Console.WriteLine(week["Fri"]);
    System.Console.WriteLine(week["Made-upDay"]);
  }
}

 

  输出

  5

  -1

  可靠编程

  提高索引器的安全性和可靠性有两种主要的方法:

  确保结合某一类型的错误处理策略,以处理万一客户端代码传入无效索引值的情况。在本主题前面的第一个示例中,TempRecord类提供了Length属性,使客户端代码能够在将输入传递给索引器之前对其进行验证。也可以将错误处理代码放入索引器自身内部。确保为用户记录在索引器的访问器中引发的任何异常。有关更多信息,请参见异常设计准则。

  应当为get和set访问器的可访问性设置尽可能多的限制。这一点对set

  ondiary/archive/2008/08/15/net.html

 

 

  此索引器将具有名称TheItem。不提供名称属性将生成Item默认名称。

  示例1

  说明

  下面的示例说明如何声明私有数组字段、temps和索引器。使用索引器可直接访问实例tempRecord[i]。另一种使用索引器的方法是将数组声明为public成员并直接访问它的成员tempRecord.temps[i]。

  请注意,当计算索引器的访问时(例如,在Console.Write语句中),将调用get访问器。因此,如果get访问器不存在,将发生编译时错误。

  代码

  C#复制代码

classTempRecord
{
  //Arrayoftemperaturevalues
  privatefloat[]temps=newfloat[10]{56.2F,56.7F,56.5F,56.9F,58.8F,
                      61.3F,65.9F,62.1F,59.2F,57.5F};
  //Auto-ImplementedProperty
  System.DateTimedate{get;set;}
  //Toenableclientcodetovalidateinput
  //whenaccessingyourindexer.
  publicintLength
  {
    get{returntemps.Length;}
  }
  //Indexerdeclaration.
  //Inputparameterisvalidatedbyclient
  //codebeforebeingpassedtotheindexer.
  publicfloatthis[intindex]
  {
    get
    {
      returntemps[index];
    }
    set
    {
      temps[index]=value;
    }
  }
}
classMainClass
{
  staticvoidMain()
  {
    TempRecordtempRecord=newTempRecord();
    //Usetheindexer'ssetaccessor
    tempRecord[3]=58.3F;
    tempRecord[5]=60.1F;
    //Usetheindexer'sgetaccessor
    for(inti=0;i<10;i++)
    {
      //Thisexamplevalidatestheinputontheclientside.Youmay
      //choosetovalidateitintheclassthatimplementstheindexer,andthrowan
      //exceptionorreturnanerrorcodeinthecaseofinvalidinput.
      if(i<tempRecord.Length)
      {
        System.Console.WriteLine("Element#{0}={1}",i,tempRecord[i]);
      }
      else
      {
        System.Console.WriteLine("Indexvalueof{0}isoutofrange",i);
      }
    }
    //Uncommentthiscodetoseehowthe.NETFrameworkhandlesindexerexceptions
    //try
    //{
    //  System.Console.WriteLine("Element#{0}={1}",tempRecord[tempRecord.Length]);
    //}
    //catch(System.ArgumentOutOfRangeExceptione)
    //{
    //  System.Console.WriteLine(e);
    //}
  }
}



相关教程