Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Type Casting | Dealing with Data Types
C# Basics

Basic Type CastingBasic Type Casting

Type Casting is the process of converting a value of one datatype to another. Type Casting is usually possible between numerically represented values. For-example we can convert a float to an int and an int to a float. However converting a string to an int does not make any logical sense and hence is not possible.

There are two types of typecasting, namely, Implicit Type Casting, and Explicit Type Casting.

Implicit Type Casting is when a value is automatically converted from one type to another, where needed.

cs

main.cs

Whenever implicit type casting happens, it's always from a smaller to a bigger data type. The size of a data type is determined by how many bits of data it can store. For example int can store 32 bits while long can store 64 bits hence int to long implicit conversion is possible. Similarly float to double is possible as well.

The types ordered by their sizes are ordered from smallest to biggest below: char -> int -> long -> float -> double

Explicit Type Casting is when the user specifically mentions the new data type of a value to which it should be converted. Explicit Type Casting is needed when we want to convert a value of a bigger data type to a smaller one, this is because there is almost always data loss when converting a value from a bigger to a smaller data type. For example, if we convert the float 3.14 to an integer, it will give 3 as a result hence we have lost some data during this conversion. Therefore the compiler needs an explicit command from the programmer to perform such a conversion.

cs

main.cs

To fix the error in the above code, we need to explicitly cast val1 to an integer value.

The syntax of a cast is (datatype) value, for-example (int) 3.1415. So in this case we will write (int) val1:

cs

main.cs

question-icon

Which of the statements below are correct?

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 2. Розділ 11
course content

Зміст курсу

C# Basics

Basic Type CastingBasic Type Casting

Type Casting is the process of converting a value of one datatype to another. Type Casting is usually possible between numerically represented values. For-example we can convert a float to an int and an int to a float. However converting a string to an int does not make any logical sense and hence is not possible.

There are two types of typecasting, namely, Implicit Type Casting, and Explicit Type Casting.

Implicit Type Casting is when a value is automatically converted from one type to another, where needed.

cs

main.cs

Whenever implicit type casting happens, it's always from a smaller to a bigger data type. The size of a data type is determined by how many bits of data it can store. For example int can store 32 bits while long can store 64 bits hence int to long implicit conversion is possible. Similarly float to double is possible as well.

The types ordered by their sizes are ordered from smallest to biggest below: char -> int -> long -> float -> double

Explicit Type Casting is when the user specifically mentions the new data type of a value to which it should be converted. Explicit Type Casting is needed when we want to convert a value of a bigger data type to a smaller one, this is because there is almost always data loss when converting a value from a bigger to a smaller data type. For example, if we convert the float 3.14 to an integer, it will give 3 as a result hence we have lost some data during this conversion. Therefore the compiler needs an explicit command from the programmer to perform such a conversion.

cs

main.cs

To fix the error in the above code, we need to explicitly cast val1 to an integer value.

The syntax of a cast is (datatype) value, for-example (int) 3.1415. So in this case we will write (int) val1:

cs

main.cs

question-icon

Which of the statements below are correct?

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 2. Розділ 11
some-alt