Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type Conversions Implicit Conversion Explicit Conversion.

Similar presentations


Presentation on theme: "Type Conversions Implicit Conversion Explicit Conversion."— Presentation transcript:

1 Type Conversions Implicit Conversion Explicit Conversion

2 Intro Value must be compatible with the variable. If not, we have to make a type conversion. Some data types can be converted to an other and some can not. We can lose data during unsafe conversion e.g. assigning a floating point number to an integer. In that case we lose fraction.

3 Implicit Conversions Numeric

4 Properties Occurs between compatible types. Transfers data from a smaller type to a bigger type. No data loss. No extra operation. Automatic type conversion.

5 Compatible types

6 Example Implicit Conversion No data loss

7 Explicit Conversion Numeric Character Character Numeric

8 Properties Occurs between incompatible types. May lose data. Extra coding needed. Use with caution.

9 Example

10 Exercise Assign a value to integer variable “source” that can not fit in to short type. Comment on the result.

11 Numeric Character.ToString() method. Can be used with numeric types. Converts numeric value to “ string ” type. There is also a formatting option.

12 Example Output : 123s

13 Formatting example “ NumberFormatInfo ” class. Used especially with floating point numbers. Resides under System.Globalization namespace. Formatting properties Number decimal digits Number decimal separator Number group separator

14 Formatting example Output: 12.345,67

15 Character Numeric “ Convert ” class “ Explicit Conversion ” Used in conversion of multiple types. There should be compatible types between conversions.

16 Example Output: 132

17 Example – 2

18 Console.ReadLine() Used for getting input from screen. String type value is returned. Returns when user press Enter key. Later, return value can be converted to other types.

19 Console.ReadLine()


Download ppt "Type Conversions Implicit Conversion Explicit Conversion."

Similar presentations


Ads by Google