Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / docs / ecma334 / 8.2.2.xml
1 <?xml version="1.0"?>
2 <clause number="8.2.2" title="Conversions" informative="true">
3   <paragraph>The predefined types also have predefined conversions. For instance, conversions exist between the predefined types <keyword>int</keyword> and <keyword>long</keyword>. C# differentiates between two kinds of conversions: implicit conversions and explicit conversions. Implicit conversions are supplied for conversions that can safely be performed without careful scrutiny. For instance, the conversion from <keyword>int</keyword> to <keyword>long</keyword> is an implicit conversion. This conversion always succeeds, and never results in a loss of information. The following example <code_example><![CDATA[
4 using System;  
5 class Test  
6 {  
7    static void Main() {  
8       int intValue = 123;  
9       long longValue = intValue;  
10       Console.WriteLine("{0}, {1}", intValue, longValue);  
11    }  
12 }  
13 ]]></code_example>implicitly converts an <keyword>int</keyword> to a <keyword>long</keyword>. </paragraph>
14   <paragraph>In contrast, explicit conversions are performed with a cast expression. The example <code_example><![CDATA[
15 using System;  
16 class Test  
17 {  
18    static void Main() {  
19       long longValue = Int64.MaxValue;  
20       int intValue = (int) longValue;  
21       Console.WriteLine("(int) {0} = {1}", longValue, intValue);  
22    }  
23 }  
24 ]]></code_example>uses an explicit conversion to convert a <keyword>long</keyword> to an <keyword>int</keyword>. The output is: <code_example><![CDATA[
25 (int) 9223372036854775807 = -1  
26 ]]></code_example>because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions. </paragraph>
27 </clause>