
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Type Conversion
Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms −
Implicit Type Conversion
The implicit conversions are performed by the C# compiler in a type-safe manner. For example, a value can be converted from one data type to another without requiring explicit casting, from smaller to larger integral types, or from derived classes to base classes.
Example
In this example, we demonstrate implicit casting, which occurs automatically when a smaller size type passed to a larger size type −
using System; namespace MyExample { class Example { static void Main(string[] args) { int myInt = 9; // Automatic casting: int to double double myDouble = myInt; Console.WriteLine(myInt); Console.WriteLine(myDouble); } } }
When the above code is compiled and executed, it produces the following result −
9 9
Explicit Type Conversion
The explicit conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
Example 1
The following example shows an explicit type conversion, Here we convert double to int −
using System; namespace TypeConversionApplication { class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // cast double to int. i = (int)d; Console.WriteLine(i); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
5673
Example 2
In the following example, we manually cast an int to a float in C# using explicit type conversion:
using System; namespace MyApplication { class Program { static void Main(string[] args) { int myInt = 10; // Manual casting: int to float float myFloat = (float) myInt; Console.WriteLine(myInt); Console.WriteLine(myFloat); } } }
When the above code is compiled and executed, it produces the following result −
10 10
Type Conversion Using Convert Class
The Convert
class in C# provides methods for converting various data types explicitly.
Example
using System; namespace ConversionExample { class Program { static void Main(string[] args) { string str = "123"; int num = Convert.ToInt32(str); Console.WriteLine(num); } } }
When the above code is compiled and executed, it produces the following result −
123
Type Conversion Using Parse() Method
The Parse()
method is used to convert a string representation of a number into its respective data type.
Example
using System; namespace ParseExample { class Program { static void Main(string[] args) { string str = "456"; int num = int.Parse(str); Console.WriteLine(num); } } }
When the above code is compiled and executed, it produces the following result −
456
Type Conversion Using TryParse() Method
The TryParse()
method safely converts a string to a numeric data type and returns a boolean indicating success or failure.
Example
using System; namespace TryParseExample { class Program { static void Main(string[] args) { string str = "789"; if (int.TryParse(str, out int result)) { Console.WriteLine(result); } else { Console.WriteLine("Conversion failed."); } } } }
When the above code is compiled and executed, it produces the following result −
789
C# Type Conversion Methods
C# provides the following built-in type conversion methods −
Sr.No. | Methods & Description |
---|---|
1 |
ToBoolean Converts a type to a Boolean value, where possible. |
2 |
ToByte Converts a type to a byte. |
3 |
ToChar Converts a type to a single Unicode character, where possible. |
4 |
ToDateTime Converts a type (integer or string type) to date-time structures. |
5 |
ToDecimal Converts a floating point or integer type to a decimal type. |
6 |
ToDouble Converts a type to a double type. |
7 |
ToInt16 Converts a type to a 16-bit integer. |
8 |
ToInt32 Converts a type to a 32-bit integer. |
9 |
ToInt64 Converts a type to a 64-bit integer. |
10 |
ToSbyte Converts a type to a signed byte type. |
11 |
ToSingle Converts a type to a small floating point number. |
12 |
ToString Converts a type to a string. |
13 |
ToType Converts a type to a specified type. |
14 |
ToUInt16 Converts a type to an unsigned int type. |
15 |
ToUInt32 Converts a type to an unsigned long type. |
16 |
ToUInt64 Converts a type to an unsigned big integer. |
Example
The following example converts various value types to string type −
using System; namespace TypeConversionApplication { class StringConversion { static void Main(string[] args) { int i = 75; float f = 53.005f; double d = 2345.7652; bool b = true; Console.WriteLine(i.ToString()); Console.WriteLine(f.ToString()); Console.WriteLine(d.ToString()); Console.WriteLine(b.ToString()); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
75 53.005 2345.7652 True