⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 17.8.htm

📁 This ECMA Standard specifies the form and establishes the interpretation of programs written in the
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="17.7.4.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="17.8.1.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="16.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="18.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="17.htm">17</a></span><span class="clause-title-previous"> Classes</span></span><span class="clause-depth"><a href="17.7.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="17.9.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">17.8</span><span class="clause-title"> Indexers</span></span><span class="locator">
     Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>An indexer is a member that enables an object to be indexed in the same way as an array.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P1S2"></a>Indexers are declared using indexer-declarations: <span class="grammar-production"><span class="name"><a name="indexer-declaration"></a>indexer-declaration</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attributes">attributes</a></span><sub>opt</sub> <span class="non-terminal"><a href="17.8.htm#indexer-modifiers">indexer-modifiers</a></span><sub>opt</sub> <span class="non-terminal"><a href="17.8.htm#indexer-declarator">indexer-declarator</a></span> <span class="terminal">{</span> <span class="non-terminal"><a href="17.6.2.htm#accessor-declarations">accessor-declarations</a></span> <span class="terminal">}</span> </span></span><span class="grammar-production"><span class="name"><a name="indexer-modifiers"></a>indexer-modifiers</span> : <span class="rhs"><span class="non-terminal"><a href="17.8.htm#indexer-modifier">indexer-modifier</a></span> </span><span class="rhs"><span class="non-terminal"><a href="17.8.htm#indexer-modifiers">indexer-modifiers</a></span> <span class="non-terminal"><a href="17.8.htm#indexer-modifier">indexer-modifier</a></span> </span></span><span class="grammar-production"><span class="name"><a name="indexer-modifier"></a>indexer-modifier</span> : <span class="rhs"><span class="keyword">new</span> </span><span class="rhs"><span class="keyword">public</span> </span><span class="rhs"><span class="keyword">protected</span> </span><span class="rhs"><span class="keyword">internal</span> </span><span class="rhs"><span class="keyword">private</span> </span><span class="rhs"><span class="keyword">virtual</span> </span><span class="rhs"><span class="keyword">sealed</span> </span><span class="rhs"><span class="keyword">override</span> </span><span class="rhs"><span class="keyword">abstract</span> </span><span class="rhs"><span class="keyword">extern</span> </span></span><span class="grammar-production"><span class="name"><a name="indexer-declarator"></a>indexer-declarator</span> : <span class="rhs"><span class="non-terminal"><a href="11.htm#type">type</a></span> <span class="keyword">this</span> <span class="terminal">[</span> <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span> <span class="terminal">]</span> </span><span class="rhs"><span class="non-terminal"><a href="11.htm#type">type</a></span> <span class="non-terminal"><a href="11.2.htm#interface-type">interface-type</a></span> <span class="terminal">.</span> <span class="keyword">this</span> <span class="terminal">[</span> <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span> <span class="terminal">]</span> </span></span></span></span><span class="locator">
     Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>An <span class="non-terminal"><a href="17.8.htm#indexer-declaration">indexer-declaration</a></span> may include a set of attributes (<a href="24.htm">&#167;24</a>) and a valid combination of the four access modifiers (<a href="17.2.3.htm">&#167;17.2.3</a>), the new (<a href="17.2.2.htm">&#167;17.2.2</a>), virtual (<a href="17.5.3.htm">&#167;17.5.3</a>), override (<a href="17.5.4.htm">&#167;17.5.4</a>), sealed (<a href="17.5.5.htm">&#167;17.5.5</a>), abstract (<a href="17.5.6.htm">&#167;17.5.6</a>), and extern (<a href="17.5.7.htm">&#167;17.5.7</a>) modifiers.</span> </span><span class="locator">
     Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>Indexer declarations are subject to the same rules as method declarations (<a href="17.5.htm">&#167;17.5</a>) with regard to valid combinations of modifiers, with the one exception being that the static modifier is not permitted on an indexer declaration.</span> </span><span class="locator">
     Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The modifiers virtual, override, and abstract are mutually exclusive except in one case.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>The abstract and override modifiers may be used together so that an abstract indexer can override a virtual one.</span> </span><span class="locator">
     Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>The type of an indexer declaration specifies the element type of the indexer introduced by the declaration.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a>Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P5S3"></a>For an explicit interface member implementation, the type is followed by an <span class="non-terminal"><a href="11.2.htm#interface-type">interface-type</a></span>, a ".", and the keyword this.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P5S4"></a>Unlike other members, indexers do not have user-defined names.</span> </span><span class="locator">
     Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>The <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span> specifies the parameters of the indexer.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P6S2"></a>The formal parameter list of an indexer corresponds to that of a method (<a href="17.5.1.htm">&#167;17.5.1</a>), except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted.</span> </span><span class="locator">
     Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>The type of an indexer and each of the types referenced in the <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span> must be at least as accessible as the indexer itself (<a href="10.5.4.htm">&#167;10.5.4</a>).</span> </span><span class="locator">
     Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>The <span class="non-terminal"><a href="17.6.2.htm#accessor-declarations">accessor-declarations</a></span> (<a href="17.6.2.htm">&#167;17.6.2</a>), which must be enclosed in "{" and "}" tokens, declare the accessors of the indexer.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P8S2"></a>The accessors specify the executable statements associated with reading and writing indexer elements.</span> </span><span class="locator">
     Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer element is not classified as a variable.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P9S2"></a>Thus, it is not possible to pass an indexer element as a ref or out argument.</span> </span><span class="locator">
     Paragraph 10</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P10S1"></a>The <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span> of an indexer defines the signature (<a href="10.6.htm">&#167;10.6</a>) of the indexer.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P10S2"></a>Specifically, the signature of an indexer consists of the number and types of its formal parameters.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P10S3"></a>The element type and names of the formal parameters are not part of an indexer's signature.</span> </span><span class="locator">
     Paragraph 11</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P11S1"></a>The signature of an indexer must differ from the signatures of all other indexers declared in the same class.</span> </span><span class="locator">
     Paragraph 12</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P12S1"></a>Indexers and properties are very similar in concept, but differ in the following ways: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P12S2"></a> A property is identified by its name, whereas an indexer is identified by its signature.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P12S3"></a> A property is accessed through a <span class="non-terminal"><a href="14.5.2.htm#simple-name">simple-name</a></span> (<a href="14.5.2.htm">&#167;14.5.2</a>) or a <span class="non-terminal"><a href="14.5.4.htm#member-access">member-access</a></span> (<a href="14.5.4.htm">&#167;14.5.4</a>), whereas an indexer element is accessed through an <span class="non-terminal"><a href="14.5.6.htm#element-access">element-access</a></span> (<a href="14.5.6.2.htm">&#167;14.5.6.2</a>).</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P12S4"></a> A property can be a static member, whereas an indexer is always an instance member.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P12S5"></a> A get accessor of a property corresponds to a method with no parameters, whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.</span> </li><li><span class="sentence"><span class="sentence-number">6</span> <a name="P12S6"></a> A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.</span> </li><li><span class="sentence"><span class="sentence-number">7</span> <a name="P12S7"></a> It is a compile-time error for an indexer accessor to declare a local variable with the same name as an indexer parameter.</span> </li><li><span class="sentence"><span class="sentence-number">8</span> <a name="P12S8"></a> In an overriding property declaration, the inherited property is accessed using the syntax base.P, where P is the property name.</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P12S9"></a>In an overriding indexer declaration, the inherited indexer is accessed using the syntax base[E], where E is a comma-separated list of expressions.</span> </li></ul></span><span class="locator">
     Paragraph 13</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P13S1"></a>Aside from these differences, all rules defined in <a href="17.6.2.htm">&#167;17.6.2</a> and <a href="17.6.3.htm">&#167;17.6.3</a> apply to indexer accessors as well as to property accessors.</span> </span><span class="locator">
     Paragraph 14</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P14S1"></a>When an indexer declaration includes an extern modifier, the indexer is said to be an external indexer.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P14S2"></a>Because an external indexer declaration provides no actual implementation, each of its  <span class="non-terminal"><a href="17.6.2.htm#accessor-declarations">accessor-declarations</a></span> consists of a semicolon.</span> </span><span class="paragraph"><span class="example">[Example: The example below declares a BitArray class that implements an indexer for accessing the individual bits in the bit array. <pre class="code-example">
using System;  
class BitArray  
{  
   int[] bits;  
   int length;  
   public BitArray(int length) {  
      if (length &lt; 0) throw new ArgumentException();  
      bits = new int[((length - 1) &gt;&gt; 5) + 1];  
      this.length = length;  
   }  
   public int Length {  
      get { return length; }  
   }  
   public bool this[int index] {  
      get {  
         if (index &lt; 0 || index &gt;= length) {  
            throw new IndexOutOfRangeException();  
         }  
         return (bits[index &gt;&gt; 5] &amp; 1 &lt;&lt; index) != 0;  
      }  
      set {  
         if (index &lt; 0 || index &gt;= length) {  
            throw new IndexOutOfRangeException();  
         }  
         if (value) {  
            bits[index &gt;&gt; 5] |= 1 &lt;&lt; index;  
         }  
         else {  
            bits[index &gt;&gt; 5] &amp;= ~(1 &lt;&lt; index);  
         }  
      }  
   }  
}  
</pre></span></span><span class="paragraph"><span class="example">An instance of the BitArray class consumes substantially less memory than a corresponding bool[] (since each value of the former occupies only one bit instead of the latter's one <span class="keyword">byte</span>), but it permits the same operations as a bool[]. </span></span><span class="paragraph"><span class="example">The following CountPrimes class uses a BitArray and the classical "sieve" algorithm to compute the number of primes between 1 and a given maximum: <pre class="code-example">
class CountPrimes  
{  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -