Table of contents
1.
Introduction
2.
Type Casting
3.
Implicit conversion
3.1.
Output
4.
Explicit conversions
4.1.
Output
5.
Frequently asked questions
5.1.
What is type casting?
5.2.
What is auto widening and explicit narrowing?
5.3.
What are auto-up type casting and explicit downcasting? 
5.4.
What is boxing and unboxing?
5.5.
Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Type Casting and Type Conversion in C#

Author Saksham Gupta
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# forms the backbone of any coding interview. Thus it's very important to have a good grasp of this language. But don't you worry about any of it. Ninjas are here for you, and today we will be going to discuss ‘Type casting and Type conversion in C#’ 

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Type Casting

Type Casting is the method through which a programmer or user converts one data type into another while writing computer code in any programming language. It is manually used by the programmer to convert one data type to another. If we wish to change the target data type to anything else, we utilize it. It's important to keep in mind that the destination data type must be smaller than the source data type. As a result, it's also known as a narrowing conversion.

There are two types of type casting in C#.

Implicit Type- C# does these implicit type casting in a type-safe manner. Conversions from smaller to bigger integral types, as well as conversions from derived classes to base classes, are examples.

Explicit Type-Type casting that are done explicitly by users using pre-defined functions are known as explicit type conversion. A cast operator is required for explicit conversions.

Implicit conversion

When passing a lower size type to a larger size type, implicit type casting is performed automatically. The following table shows the types which can be converted to one another form type conversion.

From

To

byte

short, int, long, float, double

short

int, long, float, double

int

long, float, double

long

float, double

float

double

For example

int varINT = 9;

 
// Automatic casting.
double varDOUBLE = varINT;     

 
Console.WriteLine(varINT);    

 
Console.WriteLine(varDOUBLE);  

Output

9
0.9

Explicit conversions

Manual explicit type casting is done by putting the type in parentheses in front of the value. The following table shows the different functions for type conversion.

Function Description 
ToBoolean Converts to boolean
ToChar Converts to char
ToByte Converts to byte
ToDecimal Converts to decimal
ToDouble Converts to double
ToInt16 Converts to signed 16-bit int
ToInt32 Converts to signed 32-bit int
ToInt64 Converts to signed 64-bit int
ToString Converts to string
ToUInt16 Converts to unsigned 16-bit int
ToUInt32 Converts to unsigned 32-bit int
ToUInt64 Converts to unsigned 64-bit int

For example

int VarInt = 10;
double VarDouble = 5.25;
bool VarBool = true;

 
Console.WriteLine(Convert.ToString(VarInt));    
Console.WriteLine(Convert.ToDouble(VarInt));    
Console.WriteLine(Convert.ToInt32(VarDouble));  
Console.WriteLine(Convert.ToString(VarBool));   

Output

“10”
10.00
5
“true”

Frequently asked questions

What is type casting?

Type casting is the method through which a programmer or user converts one data type into another while writing computer code in any programming language. It is manually used by the programmer to convert one data type to another. If we wish to change the target data type to anything else, we utilize it. It's important to keep in mind that the destination data type must be smaller than the source data type. 
 

What is auto widening and explicit narrowing?

The information is automatically cast from a small primitive type to a large primitive type. This is referred to as auto-widening. In other words, the data is automatically converted from byte to short, short to int, int to long, long to float, and float to double. You must cast the data from a large primitive type to a small primitive type directly. i.e., you must convert the data from double to float, float to long, long to int, int to short, and short to byte directly. The explicit narrowing is the term for this.
 

What are auto-up type casting and explicit downcasting? 

A subclass object can be automatically cast to a superclass object. This is referred to as an auto-up casting. It is termed explicit down casting when an object of a superclass type is explicitly cast to a subclass type.
 

What is boxing and unboxing?

Boxing is the process of wrapping primitive material into a wrapper class object. Unboxing is the process of converting a wrapper class object into primitive content.
 

Are there any other Data Structures and Algorithms content in Coding Ninjas Studio?

Yes, Coding Ninjas Studio lets you practice coding while also answering common interview questions. The more we practice, the more probable it is that we will land a job at our ideal firm.

Conclusion

In this article, we have extensively discussed the 'Typecasting and Type conversion in C#’ We hope that this blog has helped you enhance your knowledge of ’Type Conversion’ If you would like to learn more, check out our articles in Library, where you can find everything about courses,  Interview Experiences, and other guided paths. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass