Type Conversion in Expressions : Data Type Cast : Data Type C# Examples


C# Examples » Data Type » Data Type Cast »

 

Type Conversion in Expressions





C#'s type promotion rules. Here is the algorithm that they define for binary operations:

IF one operand is a decimal, THEN the other operand is promoted to decimal (unless it is of type float or double, in which case an error results).
ELSE IF one operand is a double, the second is promoted to double.
ELSE IF one operand is a float, the second is promoted to float.
ELSE IF one operand is a ulong, the second is promoted to ulong (unless it is of type sbyte, short, int, or long, in which case an error results).
ELSE IF one operand is a long, the second is promoted to long.
ELSE IF one operand is a uint and the second is of type sbyte, short, or int, both are promoted to long.
ELSE IF one operand is a uint, the second is promoted to uint.
ELSE both operands are promoted to int.

Quote from Book: C# The Complete Reference
 Paperback: 933 pages
 Publisher: Osborne/McGraw-Hill (March 8, 2002)
 Language: English
 ISBN-10: 0072134852
 ISBN-13: 978-0072134858
A cast has this general form:




    
(target-type)  expression
    
   
  
   

When a cast involves a narrowing conversion, information might be lost.



HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Type
» Data Type Cast