Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 8 Language Overviewprevious at this levelnext at this level 8.2 Typesprevious at this levelnext at this level 8.2.2 ConversionsThe predefined types also have predefined conversions. For instance, conversions exist between the predefined types int and long. 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 int to long is an implicit conversion. This conversion always succeeds, and never results in a loss of information. The following example
using System;  
class Test  
{  
   static void Main() {  
      int intValue = 123;  
      long longValue = intValue;  
      Console.WriteLine("{0}, {1}", intValue, longValue);  
   }  
}  
implicitly converts an int to a long.
In contrast, explicit conversions are performed with a cast expression. The example
using System;  
class Test  
{  
   static void Main() {  
      long longValue = Int64.MaxValue;  
      int intValue = (int) longValue;  
      Console.WriteLine("(int) {0} = {1}", longValue, intValue);  
   }  
}  
uses an explicit conversion to convert a long to an int. The output is:
(int) 9223372036854775807 = -1  
because an overflow occurs. Cast expressions permit the use of both implicit and explicit conversions.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS