📄 8.2.1.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="8.2.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="8.2.2.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="7.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="9.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="8.htm">8</a></span><span class="clause-title-previous"> Language Overview</span></span><span class="clause-depth"><a href="8.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="8.3.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="8.2.htm">8.2</a></span><span class="clause-title-previous"> Types</span></span><span class="clause-depth"><a href="8.2.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="8.2.2.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">8.2.1</span><span class="clause-title"> Predefined types</span></span><span class="informative"><span class="paragraph">C# provides a set of predefined types, most of which will be familiar to C and C++ developers. </span><span class="paragraph">The predefined reference types are object and string. The type object is the ultimate base type of all other types. The type string is used to represent Unicode string values. Values of type string are immutable. </span><span class="paragraph">The predefined value types include signed and unsigned integral types, floating-point types, and the types <span class="keyword">bool</span>, <span class="keyword">char</span>, and <span class="keyword">decimal</span>. The signed integral types are <span class="keyword">sbyte</span>, <span class="keyword">short</span>, <span class="keyword">int</span>, and long; the unsigned integral types are <span class="keyword">byte</span>, <span class="keyword">ushort</span>, <span class="keyword">uint</span>, and ulong; and the floating-point types are <span class="keyword">float</span> and <span class="keyword">double</span>. </span><span class="paragraph">The <span class="keyword">bool</span> type is used to represent boolean values: values that are either true or false. The inclusion of <span class="keyword">bool</span> makes it easier to write self-documenting code, and also helps eliminate the all-too-common C++ coding error in which a developer mistakenly uses "=" when "==" should have been used. In C#, the example <pre class="code-example">
int i = ...;
F(i);
if (i = 0) // Bug: the test should be (i == 0)
G();
</pre>results in a compile-time error because the expression i = 0 is of type <span class="keyword">int</span>, and if statements require an expression of type <span class="keyword">bool</span>. </span><span class="paragraph">The <span class="keyword">char</span> type is used to represent Unicode characters. A variable of type <span class="keyword">char</span> represents a single 16-bit Unicode character. </span><span class="paragraph">The <span class="keyword">decimal</span> type is appropriate for calculations in which rounding errors caused by floating point representations are unacceptable. Common examples include financial calculations such as tax computations and currency conversions. The <span class="keyword">decimal</span> type provides 28 significant digits. </span><span class="paragraph">The table below lists the predefined types, and shows how to write literal values for each of them.
<table border="1">
<tr>
<th align="left">Type</th>
<th align="left">Description</th>
<th align="left">Example</th>
</tr>
<tr>
<td>object</td>
<td>The ultimate base type of all other types</td>
<td>object o = null;</td>
</tr>
<tr>
<td>string</td>
<td>String type; a string is a sequence of Unicode characters</td>
<td>string s = "hello";</td>
</tr>
<tr>
<td>sbyte</td>
<td>8-bit signed integral type</td>
<td>sbyte val = 12;</td>
</tr>
<tr>
<td>short</td>
<td>16-bit signed integral type</td>
<td>short val = 12;</td>
</tr>
<tr>
<td>int</td>
<td>32-bit signed integral type</td>
<td>int val = 12;</td>
</tr>
<tr>
<td>long</td>
<td>64-bit signed integral type</td>
<td>long val1 = 12;</br>long val2 = 34L;</td>
</tr>
<tr>
<td>byte</td>
<td>8-bit unsigned integral type</td>
<td>byte val1 = 12;</td>
</tr>
<tr>
<td>ushort</td>
<td>16-bit unsigned integral type</td>
<td>ushort val1 = 12;</td>
</tr>
<tr>
<td>uint</td>
<td>32-bit unsigned integral type</td>
<td>uint val1 = 12;</br>uint val2 = 34U;</td>
</tr>
<tr>
<td>ulong</td>
<td>64-bit unsigned integral type</td>
<td>ulong val1 = 12;</br>ulong val2 = 34U;</br>ulong val3 = 56L;</br>ulong val4 = 78UL;</td>
</tr>
<tr>
<td>float</td>
<td>Single-precision floating point type</td>
<td>float val = 1.23F;</td>
</tr>
<tr>
<td>double</td>
<td>Double-precision floating point type</td>
<td>double val1 = 1.23;</br>double val2 = 4.56D;</td>
</tr>
<tr>
<td>bool</td>
<td>Boolean type; a bool value is either true or false</td>
<td>bool val1 = true;</br>bool val2 = false;</td>
</tr>
<tr>
<td>char</td>
<td>Character type; a char value is a Unicode character</td>
<td>char val = 'h';</td>
</tr>
<tr>
<td>decimal</td>
<td>Precise decimal type with 28 significant digits</td>
<td>decimal val = 1.23M;</td>
</tr>
</table>
</span><span class="paragraph">Each of the predefined types is shorthand for a system-provided type. For example, the keyword <span class="keyword">int</span> refers to the struct System.Int32. As a matter of style, use of the keyword is favored over use of the complete system type name. </span><span class="paragraph">Predefined value types such as <span class="keyword">int</span> are treated specially in a few ways but are for the most part treated exactly like other structs. Operator overloading enables developers to define new struct types that behave much like the predefined value types. For instance, a Digit struct can support the same mathematical operations as the predefined integral types, and can define conversions between Digit and predefined types. </span><span class="paragraph">The predefined types employ operator overloading themselves. For example, the comparison operators == and != have different semantics for different predefined types: <ul><li> Two expressions of type <span class="keyword">int</span> are considered equal if they represent the same integer value. </li><li> Two expressions of type object are considered equal if both refer to the same object, or if both are null. </li><li> Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null. </li></ul></span><span class="paragraph">The example <pre class="code-example">
using System;
class Test
{
static void Main() {
string s = "Test";
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == (object)t);
}
}
</pre>produces the output <pre class="code-example">
True
False
</pre>because the first comparison compares two expressions of type string, and the second comparison compares two expressions of type object. </span></span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -