C Sharp Type Casting

In this tutorial, you will learn about C Sharp type casting to change the type of variables. Casting types are implicit and explicit to convert the variable from one type to others.

What is Type Casting in C Sharp (C#)

Type casting is the way of assigning a value of one data type to another data type. After we create a variable in C#, we cannot create it again. However, there are some situations where we need to change the data type.

If the conversion is compatible with the data type, it can be automatically convertible to the other data type in C#. If the conversion is not with the data type, you need to manually convert it to another data type.

Types of Casting in C Sharp (C#)

There are two types of casting in C# that are given below:-

  1. Implicit Type Casting (Automatic conversion from smaller to larger data types)
  2. Explicit Type Casting (Manual conversion from larger to smaller data types)

Let’s find out these types in detail given below with examples.

Implicit Type Casting in C Sharp (Automatic Conversion C#)

In an implicit type casting, one data type is automatically converted to another data type during compilation in C#. This type is safe to use and you will not lose any data.

To convert the data type implicitly, you have to assign the smaller variable to the larger variable. You can mainly use implicit type casting to convert smaller data types to larger data types.

Example

Output

myNum value is: 11
myNumDbl value is: 11

Explanation
In the above example, we have first created an integer variable myNum using int data type.

After that, we have created the double data type variable with the name myNumDbl. We have also assigned its value as an integer variable myNum which is an implicit conversion.

On C# compilation, the value of int data type automatically converted to the double data type.

As it’s an implicit conversion from a smaller data type to a larger data type, there will be no data loss after conversion. int is the smaller data type and double is the larger data type in C#.

Explicit Type Casting in C Sharp (Manual Conversion C#)

In explicit data type, one data type is manually converted to another data type at the time of compilation in C#. If the types are not compatible with the conversion, you will get some errors during compilation. This data type is not safe to use as there is some data loss in the output.

To explicitly convert one data type to another, you have to place the data type before the smaller variable within the parenthesis. See the example given below to learn the method.

Example

Output

myNumDbl value is: 9.48
myNum value is: 9

The above example converts the larger data type double to the smaller data type int.

Explanation

Let’s understand the process of explicit conversion. We have to first create the larger data type double as given below.

After that, we can create a smaller data type int and assign the larger data type variable as its value. You also have to place the int data type before the larger data type within the parenthesis as given below.

On C# compilation, you will get the result as given below. The result shows some data loss after you get the output.

As it is an explicit conversion, you need to manually assign the data type with the larger variable. Also, there will be some data loss at the end of the result.

C Sharp Built-in Methods for Type Conversion

C-Sharp (C#) has some built-in methods that are useful to convert one data type to another. Below is the list of the useful method for type conversion:-

Method Name Description
ToBoolean Convert a type to a boolean value
ToChar To convert a type to character type
ToString To convert a type to string type
ToByte Convert a type to a byte value
ToDecimal For converting a type to a decimal value
ToDouble Convert a type to double value

You can use these simple methods to easily convert your data type to the required data type.

Boolean Type Conversion Using ToBoolean

If you want to convert your required data type to boolean, you can use the C# built-in method Convert.ToBoolean(). You have to pass the variable as the parameter of the method to convert its type to boolean data type.

Output

The boolean value of 21 is: True

In the above example, the value is converted to boolean. If the value is 0, it is converted to a boolean value as False. If the value is other than 0, it is converted to a boolean value as True.

String Type Conversion Using ToString

The string type conversion can be done using the C# built-in method Convert.ToString(). You have to pass the variable as its parameter to convert it into a string type value.

Output

The string value of 29 is: 29

The above output shows that the converted value is string type.

Double Type Conversion Using ToDouble

If you want to convert the value to double data type, you have to use the C-Sharp built-in method Convert.ToDouble(). Pass the variable as its argument to change its value to the double data type.

Output

The double value of 32 is: 32

The above value shows that the converted value is a double data type.

Related Posts

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.