• 字符

在.NET Framework中,字符总是表示成16位Unicode代码值,简化了国际化应用程序的开发。

数值类型与Char实例的相互转换:

1.强制类型转换,效率最高,C#允许指定转换时是checked还是unchecked

2.使用Convert类型

3.使用IConvertible接口,效率最差

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Char c;
14             int n;
15             c = (Char)65; //强制类型转换
16             Console.WriteLine(c);
17             n = (int)c; //强制类型转换
18             Console.WriteLine(n);
19 
20             c = unchecked((Char)(65536 + 65));
21             Console.WriteLine(c);
22 
23             c = Convert.ToChar(65); //使用Convert类型
24             Console.WriteLine(c);
25             n = Convert.ToInt32(c);
26             Console.WriteLine(n);
27             try
28             {
29                 c = Convert.ToChar(70000);
30                 Console.WriteLine(c);
31             }
32             catch(OverflowException e)
33             {
34                 Console.WriteLine("overflow...");
35             }
36 
37             c = ((IConvertible)65).ToChar(null); //使用IConvertible转换
38             Console.WriteLine(c);
39             n = ((IConvertible)c).ToChar(null);
40             Console.WriteLine(n);
41             Console.ReadKey();
42         }
43     }
44 }
View Code

相关文章:

  • 2021-08-24
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-06-18
猜你喜欢
  • 2022-01-01
  • 2022-12-23
  • 2021-05-20
  • 2022-01-29
  • 2021-08-13
  • 2022-12-23
相关资源
相似解决方案