[C# Tutorial] C# Type Conversion
C# Type Conversion
Type conversion is fundamentally type casting, or converting data from one type to another. In C#, type casting comes in two forms:
Implicit type conversions - these are the default conversions that C# performs in a safe manner. For example, converting from a small integer type to a large integer type, and from a derived class to a base class.
Explicit type conversions - These conversions are done explicitly by the user using predefined functions. Explicit conversion requires a cast operator.
The following example shows an explicit type conversion:
namespace TypeConversionApplication { class ExplicitConversion { static void Main(string[] args) { double d = 5673.74; int i; // 强制转换 double 为 int i = (int)d; Console.WriteLine(i); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following results:
5673
C# type Conversion Methods
C# provides the following built-in type conversion methods:
Serial Number
Method& Description
1 ToBoolean
Convert the type to Boolean if possible.
2 ToByte
Convert the type to byte type.
3 ToChar
Convert the type to a single Unicode character type if possible.
4 ToDateTime
Convert the type (integer or string type) to a date-time structure.
5 ToDecimal
Convert floating point or integer type to decimal type.
6 ToDouble
Convert the type to double precision floating point type.
7 ToInt16
Convert the type to a 16-bit integer type.
8 ToInt32
Convert the type to a 32-bit integer type.
9 ToInt64
Convert the type to a 64-bit integer type.
10 ToSbyte
Convert the type to a signed byte type.
11 ToSingle
Convert the type to a small floating point number type.
12 ToString
Convert the type to string type.
13 ToType
Convert the type to the specified type.
14 ToUInt16
Convert the type to a 16-bit unsigned integer type.
15 ToUInt32
Convert the type to a 32-bit unsigned integer type.
16 ToUInt64
Convert the type to a 64-bit unsigned integer type.
The following example converts different value types into string types:
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 results:
75 53.005 2345.7652 True
The above is the content of [c# tutorial] C# type conversion. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.
