Type Conversion in Expressions : Data Type Cast « Data Type « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Data Type » Data Type Cast 
2.50.1.Type Conversion in Expressions

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

  1. 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).
  2. ELSE IF one operand is a double, the second is promoted to double.
  3. ELSE IF one operand is a float, the second is promoted to float.
  4. 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).
  5. ELSE IF one operand is a long, the second is promoted to long.
  6. ELSE IF one operand is a uint and the second is of type sbyte, short, or int, both are promoted to long.
  7. ELSE IF one operand is a uint, the second is promoted to uint.
  8. 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-typeexpression

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

2.50.Data Type Cast
2.50.1.Type Conversion in Expressions
2.50.2.Automatic Conversions
2.50.3.The use of the cast operator: how information loss can occur when explicitly converting a variable of one type to another
2.50.4.Automatic conversion from long to double
2.50.5.Cast an int into a double
2.50.6.Cast an int into a byte, no data lost
2.50.7.Cast an int into a byte, data lost
2.50.8.Cast a uint into a short, no data lost
2.50.9.Cast a uint into a short, data lost
2.50.10.Cast a long into a uint, no data lost
2.50.11.cast a long into a uint, data lost
2.50.12.Cast an int into a char
2.50.13.Cast byte back for byte calculation
2.50.14.Using casts in an expression.
2.50.15.Conversions of numeric types: checked and unchecked conversions
2.50.16.Conversions of numeric types: checked conversions block
2.50.17.Using checked and unchecked.
2.50.18.Using checked and unchecked with statement blocks.
2.50.19.Most significant bits lost
2.50.20.Raises the OverflowException exception
2.50.21.Get Type Code after casting
2.50.22.Narrow With Convert
2.50.23.Narrowing int to byte
2.50.24.Overflow Check for data type converting
2.50.25.Not Using the Cast Operator for an Implicit Cast
2.50.26.Using the Cast Operator for an Implicit Cast
2.50.27.Data Conversion
2.50.28.implicit type conversions supported in C#.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.