Monday, May 21, 2007

More About Variables - Type Conversions and Complex Variable Types

What is Type Conversion?
Converting values from one type into another. Two forms of Type Conversions are:
· Implicit Conversions à where conversion from type A to type B is possible in all circumstances, and the rules for performing the conversions are simple enough for you to trust in the compiler.
· Explicit Conversions à where conversions from type A to type B is only possible in certain circumstances or where the rules for conversions are complicated enough to merit additional processing of some kind.

Explain Implicit Conversion?
Implicit Conversion requires no work on your part and no additional code.
For Ex: the values of ushort and char are effectively interchangeable, because both store a number between 0 and 65535. you can covert values between these types implicitly.
Ex2: the bool and string have no implicit conversions, but the numeric types have a few.

The following table shows the numeric conversions that the compiler can perform implicitly (Note: chars are stored as numbers, so char counts as a numeric type):
Type --> Can Safely Be Converted To
Byte - short, ushort, int, uint, long, ulong, float, double, decimal
Sbyte - short, int, long, float, double, decimal
Short - int, long, float, double, decimal
Ushort - int, uint, long, ulong, float, double, decimal
Int - long, float, double, decimal
Uint - long, ulong, float, double, decimal
Long - float, double, decimal
Ulong - float, double, decimal
Float - double
Char - ushort, int, uint, long, ulong, float, double, decimal

Implicit Conversion Rule is: any type A whose range of possible values completely fits inside the range of possible values of type B can be implicitly converted into that type. For Example, a short type variable is capable of storing values up to 32767, and the maximum value allowed into a byte is 255, so there could be problem if you try to convert a short value into a byte value.
However you can convert the short type variable to a byte type using an explicit conversion.

Explain Explicit Conversion?
Explicit conversions occur when you explicitly ask the compiler to convert a value from one data type to another. Because of this they require extra code, and the format of this code may vary, depending on the exact conversion method.

When you try to convert a short value into a byte, what is an error message the C# compiler throws?
Cannot implicitly convert type ‘short’ to ‘byte’. An explicit conversion exists (are you missing a cast?)
To get this code to compile, you need to add the code to perform an explicit conversion. The easiest way to do this is to cast the short variable into a byte.

What is casting?
Casting basically means forcing data from one type into another. The syntax is as follows:
(destinationType)variable.

What are the different Simple Numeric Types?
· byte, ubyte
· short, ushort
· int, uint
· long, ulong
· float
· double
· decimal

What are the different Simple Variable types?
· Char
· Bool
· String

What are the different Complex Variable types?
· Enumerations
· Structs
· Arrays

No comments: