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

What is type conversion in C#?

WBOY
Release: 2023-09-11 13:49:22
forward
1077 people have browsed it

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();
      }
   }
}
Copy after login

Output

20
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template