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
course content

Contenido del Curso

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

bookBasic 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

copy
1234
int val1 = 10; long val2 = val1; // int is automatically converted to long float val3 = var1; // int is automatically converted to float double var4 = var1; // int is automatically converted to double

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

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { float val1 = 3.14f; int val2 = val1; // Error: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) Console.WriteLine(val1); Console.WriteLine(val2); } } }

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

copy
123456789101112131415
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { float val1 = 3.14f; int val2 = (int) val1; Console.WriteLine(val1); Console.WriteLine(val2); } } }
Which of the statements below are correct?

Which of the statements below are correct?

Selecciona unas respuestas correctas

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 11
some-alt