disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 8.2.4.xml
blobcdfd4e6b2de86e30eba0dd5ad37b0dd4239853d7
1 <?xml version="1.0"?>
2 <clause number="8.2.4" title="Type system unification" informative="true">
3   <paragraph>C# provides a &quot;unified type system&quot;. All types-including value types-derive from the type object. It is possible to call object methods on any value, even values of &quot;primitive&quot; types such as <keyword>int</keyword>. The example <code_example><![CDATA[
4 using System;  
5 class Test  
6 {  
7    static void Main() {  
8       Console.WriteLine(3.ToString());  
9    }  
10 }  
11 ]]></code_example>calls the object-defined ToString method on an integer literal, resulting in the output &quot;3&quot;. </paragraph>
12   <paragraph>The example <code_example><![CDATA[
13 class Test  
14 {  
15    static void Main() {  
16       int i = 123;  
17       object o = i;   // boxing  
18       int j = (int) o;  // unboxing  
19    }  
20 }  
21 ]]></code_example>is more interesting. An <keyword>int</keyword> value can be converted to object and back again to <keyword>int</keyword>. This example shows both boxing and unboxing. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location. </paragraph>
22   <paragraph>This type system unification provides value types with the benefits of object-ness without introducing unnecessary overhead. For programs that don't need <keyword>int</keyword> values to act like objects, <keyword>int</keyword> values are simply 32-bit values. For programs that need <keyword>int</keyword> values to behave like objects, this capability is available on demand. This ability to treat value types as objects bridges the gap between value types and reference types that exists in most languages. For example, a Stack class can provide Push and Pop methods that take and return object values. <code_example><![CDATA[
23 public class Stack  
24 {  
25    public object Pop() {...}  
26    public void Push(object o) {...}  
27 }  
28 ]]></code_example></paragraph>
29   <paragraph>Because C# has a unified type system, the Stack class can be used with elements of any type, including value types like <keyword>int</keyword>. </paragraph>
30 </clause>