
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is Type casting in C#?
Type casting is converting one type of data to another type. The two forms are −
Implicit type conversion − These conversions are performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion− These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
The following are the built-in type conversion methods −
Sr.No | Method & 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. |
The following example convert integer to string type −
Example
using System; namespace Demo { class Program { static void Main(string[] args) { int i = 20; Console.WriteLine(i.ToString()); Console.ReadKey(); } } }
Output
20
- Related Articles
- What is the difference between type conversion and type casting in C#?
- Type casting in JavaScript.
- Type Casting operators in C++
- Java Type Casting Examples
- Explicit Type Casting in Python Language
- const_cast in C++ - Type casting operators
- Explicit type casting operator in C++
- Difference Between Type casting and Type Conversion
- What is Casting operators in C++?
- What is the difference between up-casting and down-casting in Java?
- What is a casting expression in Java?
- What are up-casting and down-casting in Java?
- PHP Casting Variable as Object type in foreach Loop
- HTML5 data-* attribute type casting strings and numbers
- What are the differences between Widening Casting (Implicit) and Narrowing Casting (Explicit) in Java?

Advertisements