📄 25.2.htm
字号:
<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="25.1.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="25.3.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="24.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="25.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="25.htm">25</a></span><span class="clause-title-previous"> Unsafe code</span></span><span class="clause-depth"><a href="25.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="25.3.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">25.2</span><span class="clause-title"> Pointer types</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>In an unsafe context, a type (<a href="11.htm">§11</a>) may be a <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> as well as a <span class="non-terminal"><a href="11.1.htm#value-type">value-type</a></span> or a <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span>.</span> <span class="grammar-production"><span class="name"><a name="type"></a>type</span> : <span class="rhs"><span class="non-terminal"><a href="11.1.htm#value-type">value-type</a></span> </span><span class="rhs"><span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span> </span></span><span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> A <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> is written as an <span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span> or the keyword <span class="keyword">void</span>, followed by a * token: <span class="grammar-production"><span class="name"><a name="pointer-type"></a>pointer-type</span> : <span class="rhs"><span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span> <span class="terminal">*</span> </span><span class="rhs"><span class="keyword">void</span> <span class="terminal">*</span> </span></span><span class="grammar-production"><span class="name"><a name="unmanaged-type"></a>unmanaged-type</span> : <span class="rhs"><span class="non-terminal"><a href="11.htm#type">type</a></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>The type specified before the * in a pointer type is called the referent type of the pointer type.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>It represents the type of the variable to which a value of the pointer type points.</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>Unlike references (values of reference types), pointers are not tracked by the garbage collector-the garbage collector has no knowledge of pointers and the data to which they point.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>For this reason a pointer is not permitted to point to a reference or to a struct that contains references, and the referent type of a pointer must be an <span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span>.</span> </span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>An <span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span> is any type that isn't a <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span> and doesn't contain <span class="non-terminal"><a href="11.2.htm#reference-type">reference-type</a></span> fields at any level of nesting.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>In other words, an <span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span> is one of the following: </span><ul><li><span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a> <span class="keyword">sbyte</span>, <span class="keyword">byte</span>, <span class="keyword">short</span>, <span class="keyword">ushort</span>, <span class="keyword">int</span>, <span class="keyword">uint</span>, <span class="keyword">long</span>, <span class="keyword">ulong</span>, <span class="keyword">char</span>, <span class="keyword">float</span>, <span class="keyword">double</span>, <span class="keyword">decimal</span>, or <span class="keyword">bool</span>.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P4S4"></a> Any <span class="non-terminal"><a href="11.1.htm#enum-type">enum-type</a></span>.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P4S5"></a> Any <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span>.</span> </li><li><span class="sentence"><span class="sentence-number">6</span> <a name="P4S6"></a> Any user-defined <span class="non-terminal"><a href="11.1.htm#struct-type">struct-type</a></span> that contains fields of <span class="non-terminal"><a href="25.2.htm#unmanaged-type">unmanaged-type</a></span>s only.</span> </li></ul></span><span class="locator">
Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>The intuitive rule for mixing of pointers and references is that referents of references (objects) are permitted to contain pointers, but referents of pointers are not permitted to contain references.</span> </span><span class="paragraph"><span class="example">[Example: Some examples of pointer types are given in the table below:
<table border="1">
<tr>
<th>Example</th>
<th>Description</th>
</tr>
<tr>
<td>byte*</td>
<td>Pointer to byte</td>
</tr>
<tr>
<td>char*</td>
<td>Pointer to char</td>
</tr>
<tr>
<td>int**</td>
<td>Pointer to pointer to int</td>
</tr>
<tr>
<td>int*[]</td>
<td>Single-dimension array of pointers to int</td>
</tr>
<tr>
<td>void*</td>
<td>Pointer to unknown type</td>
</tr>
</table>
end example]</span> </span><span class="locator">
Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>For a given implementation, all pointer types must have the same size and representation.</span> </span><span class="paragraph"><span class="note">[Note: Unlike C and C++, when multiple pointers are declared in the same declaration, in C# the * is written along with the underlying type only, not as a prefix punctuator on each pointer name. For example: <pre class="code-example">
int* pi, pj; // NOT as int *pi, *pj;
</pre>end note]</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 value of a pointer having type T* represents the address of a variable of type T.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>The pointer indirection operator * (<a href="25.5.1.htm">§25.5.1</a>) may be used to access this variable.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P7S3"></a>For example, given a variable P of type int*, the expression *P denotes the <span class="keyword">int</span> variable found at the address contained in P.</span> </span><span class="locator">
Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>Like an object reference, a pointer may be null.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P8S2"></a>Applying the indirection operator to a null pointer results in implementation-defined behavior.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P8S3"></a>A pointer with value null is represented by all-bits-zero.</span> </span><span class="locator">
Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>The void* type represents a pointer to an unknown type.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P9S2"></a>Because the referent type is unknown, the indirection operator cannot be applied to a pointer of type void*, nor can any arithmetic be performed on such a pointer.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P9S3"></a>However, a pointer of type void* can be cast to any other pointer type (and vice versa).</span> </span><span class="locator">
Paragraph 10</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P10S1"></a>Pointer types are a separate category of types.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P10S2"></a>Unlike reference types and value types, pointer types do not inherit from object and no conversions exist between pointer types and object.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P10S3"></a>In particular, boxing and unboxing (<a href="11.3.htm">§11.3</a>) are not supported for pointers.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P10S4"></a>However, conversions are permitted between different pointer types and between pointer types and the integral types.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P10S5"></a>This is described in <a href="25.4.htm">§25.4</a>.</span> </span><span class="locator">
Paragraph 11</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P11S1"></a>A <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> may be used as the type of a volatile field (<a href="17.4.3.htm">§17.4.3</a>).</span> </span><span class="paragraph"><span class="note">[Note: Although pointers can be passed as ref or out parameters, doing so can cause undefined behavior, since the pointer may well be set to point to a local variable which no longer exists when the called method returns, or the fixed object to which it used to point, is no longer fixed. For example: <pre class="code-example">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -