Home >Backend Development >C#.Net Tutorial >What is type conversion in C#?

What is type conversion in C#?

WBOY
WBOYforward
2023-09-11 13:49:221042browse

C# 中的类型转换是什么?

Type conversion is to convert one data type to another type. The two forms are -

Implicit Type Conversions - These conversions are performed by C# in a type-safe manner. For example, conversion from small to large integer types and conversion from derived classes to base classes.

Explicit Type Conversions - These conversions are functions done explicitly by the user using predefined types. Explicit conversion requires a cast operator.

The following are the built-in type conversion methods-

tr>
Sr.No method And description
1 ToBoolean

Convert the type to Boolean (if possible) .

2 ToByte p>

Convert type to bytes.

3 ToChar

Convert the type to a single Unicode character if possible.

4 ToDateTime

Convert type (integer or string type) to date Time structure.

5 ToDecimal

Convert a floating point or integer type to a decimal type. p>

6 ToDouble

Conversion type to double type.

The following example converts an integer to a string type-

Example h2>

Live demonstration

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         int i = 20;

         Console.WriteLine(i.ToString());
         Console.ReadKey();
      }
   }
}

Output

20

The above is the detailed content of What is type conversion in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete