原文: https://zetcode.com/lang/csharp/datatypes/

在 C# 教程的这一部分中,我们将讨论数据类型。

计算机程序(包括电子表格,文本编辑器,计算器或聊天客户端)可以处理数据。 用于各种数据类型的工具是现代计算机语言的基本组成部分。 数据类型是一组值以及对这些值的允许操作。

C# 数据类型

数据类型是一组值,以及对这些值的允许操作。

C# 中的两种基本数据类型是值类型和引用类型。 基本类型(字符串除外),枚举,元组和结构是值类型。 类,字符串,接口,数组和委托是引用类型。 每种类型都有一个默认值。 引用类型在堆上创建。 引用类型的生存期由 .NET 框架管理。 引用类型的默认值为空引用。 分配给引用类型的变量会创建引用的副本,而不是引用值的副本。 值类型在栈上创建。 生存期由变量的生存期决定。 分配给值类型的变量会创建要分配的值的副本。 值类型具有不同的默认值。 例如,布尔默认值为false,十进制为 0,字符串为空字符串""

C# 布尔值

我们的世界建立了双重性。 有天地,水火,阴阳,男人和女人,爱与恨。 在 C# 中,bool数据类型是具有以下两个值之一的原始数据类型:truefalse。 这是一种基本数据类型,在计算机程序中非常常见。

快乐的父母正在等待孩子的出生。 他们为两种可能性都选择了名称。 如果要成为男孩,他们选择了约翰。 如果要成为女孩,他们会选择维多利亚。

Program.cs

  1. using System;
  2. namespace BooleanType
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Random random = new Random();
  9. bool male = Convert.ToBoolean(random.Next(0, 2));
  10. if (male)
  11. {
  12. Console.WriteLine("We will use name John");
  13. }
  14. else
  15. {
  16. Console.WriteLine("We will use name Victoria");
  17. }
  18. }
  19. }
  20. }

该程序使用随机数生成器来模拟我们的情况。

  1. Random random = new Random();

我们创建一个Random对象,该对象用于计算随机数。 它是系统名称空间的一部分。

  1. bool male = Convert.ToBoolean(random.Next(0, 2));

Next()方法返回指定范围内的随机数。 下限包括在内,上限不包括在内。 换句话说,我们收到 0 或 1。随后Convert()方法将这些值转换为布尔值,0 转换为false,1 转换为true

  1. if (male)
  2. {
  3. Console.WriteLine("We will use name John");
  4. } else
  5. {
  6. Console.WriteLine("We will use name Victoria");
  7. }

如果将male变量设置为true,我们选择名称 John。 否则,我们选择名称 Victoria。 诸如if/else语句之类的控制结构可使用布尔值。

  1. $ dotnet run
  2. We will use name John
  3. $ dotnet run
  4. We will use name John
  5. $ dotnet run
  6. We will use name Victoria

多次运行该程序将给出此示例输出。

C# 整数

整数是实数的子集。 它们写时没有小数或小数部分。 整数落在集合Z = {..., -2, -1, 0, 1, 2, ...}中。 整数是无限的。

在计算机语言中,整数是原始数据类型。 实际上,计算机只能使用整数值的子集,因为计算机的容量有限。 整数用于计算离散实体。 我们可以有 3、4、6 个人,但不能有 3.33 个人。 我们可以有 3.33 公斤。

C# 别名 .NET 类型 大小 范围
sbyte System.SByte 1 字节 -128 至 127
byte System.Byte 1 字节 0 至 255
short System.Int16 2 字节 -32,768 至 32,767
ushort System.UInt16 2 字节 0 至 65,535
int System.Int32 4 字节 -2,147,483,648 至 2,147,483,647
uint System.UInt32 4 字节 0 至 4,294,967,295
long System.Int64 8 字节 -9,223,372,036,854,775,808 至 9,223,372,036,854,775,807
ulong System.UInt64 8 字节 0 至 18,446,744,073,709,551,615

可以根据我们的需要使用这些整数类型。 没有人(也许有些圣经人除外)的年龄可以超过 120、130 岁。 然后,我们可以在程序中将byte类型用于年龄变量。 这样可以节省一些内存。

离散实体

如果我们使用整数,那么我们将处理离散实体。 我们将使用整数来计算苹果。

Program.cs

  1. using System;
  2. namespace Apples
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. int baskets = 16;
  9. int applesInBasket = 24;
  10. int total = baskets * applesInBasket;
  11. Console.WriteLine("There are total of {0} apples", total);
  12. }
  13. }
  14. }

在我们的程序中,我们计算了苹果的总量。 我们使用乘法运算。

  1. int baskets = 16;
  2. int applesInBasket = 24;

篮子数和每个篮子中的苹果数是整数值。

  1. int total = baskets * applesInBasket;

将这些值相乘,我们也得到一个整数。

  1. $ dotnet run
  2. There are total of 384 apples

这是程序的输出。

C# 整数符号

可以在 C# 中使用三种不同的表示法指定整数:十进制,十六进制和二进制。 八进制值没有符号。 据我们所知,通常使用十进制数。 十六进制数字以0x字符开头,以0b二进制文件开头。

Program.cs

  1. using System;
  2. namespace IntegerNotations
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. int num1 = 31;
  9. int num2 = 0x31;
  10. int num3 = 0b1101;
  11. Console.WriteLine(num1);
  12. Console.WriteLine(num2);
  13. Console.WriteLine(num3);
  14. }
  15. }
  16. }

在程序中,我们用三个不同的符号表示三个整数。

  1. $ dotnet run
  2. 31
  3. 49
  4. 13

默认符号是十进制。 程序以十进制显示这两个数字。 换句话说,十六进制的0x31是十进制的 49。

使用下划线

C# 允许对数字字面值使用下划线字符,以提高值的可读性。

Program.cs

  1. using System;
  2. namespace UnderscoreLiterals
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. var num1 = 234_321_000;
  9. Console.WriteLine(num1);
  10. var num2 = 0b_0110_000_100;
  11. Console.WriteLine(num2);
  12. }
  13. }
  14. }

该程序使用带下划线字符的整数字面值来提高值的可读性。

算术溢出

算术溢出是在计算产生的结果的大小大于给定寄存器或存储位置可以存储或表示的结果时发生的条件。

Program.cs

  1. using System;
  2. namespace OverFlow
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. byte a = 254;
  9. Console.WriteLine(a);
  10. a++;
  11. Console.WriteLine(a);
  12. a++;
  13. Console.WriteLine(a);
  14. a++;
  15. Console.WriteLine(a);
  16. }
  17. }
  18. }

在此示例中,我们尝试分配一个超出数据类型范围的值。 这导致算术溢出。

  1. $ dotnet run
  2. 254
  3. 255
  4. 0
  5. 1

发生溢出时,变量将重置为数据类型的下限。 (如果是字节类型,则为零。)

使用checked关键字,可以在发生溢出时强制执行异常。

Program.cs

  1. using System;
  2. namespace OverflowChecked
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. checked {
  9. byte a = 254;
  10. Console.WriteLine(a);
  11. a++;
  12. Console.WriteLine(a);
  13. a++;
  14. Console.WriteLine(a);
  15. a++;
  16. Console.WriteLine(a);
  17. }
  18. }
  19. }
  20. }

在该示例中,语句放置在checked块的主体中。

  1. $ dotnet run
  2. 254
  3. 255
  4. Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.
  5. ...

这次抛出了System.OverflowException

C# 浮点数

浮点数表示计算中的实数。 实数测量连续的数量,例如重量,高度或速度。 在 C# 中,我们有三种浮点类型:floatdoubledecimal

C# 别名 .NET 类型 大小 精度 范围
float System.Single 4 字节 7 位数 +-1.5 x 10^-45+-3.4 x 10^38
double System.Double 8 字节 15-16 位数 +-5.0 x 10^-324+-1.7 x 10^308
decimal System.Decimal 16 字节 28-29 位小数 +-1.0 x 10^-28+-7.9 x 10^28

上表给出了浮点类型的特征。

默认情况下,C# 程序中的实数是双精度的。 要使用其他类型,必须使用后缀。 float编号为F/fdecimal编号为M/m

Program.cs

  1. using System;
  2. namespace Floats
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. float n1 = 1.234f;
  9. double n2 = 1.234;
  10. decimal n3 = 1.234m;
  11. Console.WriteLine(n1);
  12. Console.WriteLine(n2);
  13. Console.WriteLine(n3);
  14. Console.WriteLine(n1.GetType());
  15. Console.WriteLine(n2.GetType());
  16. Console.WriteLine(n3.GetType());
  17. }
  18. }
  19. }

在上面的程序中,我们对浮点数使用三种不同的字面值符号。

  1. float n1 = 1.234f;

f后缀用于float数字。

  1. double n2 = 1.234;

如果我们不使用后缀,则为double数字。 我们可以选择使用d后缀。

  1. Console.WriteLine(n1.GetType());

GetType()方法返回数字的类型。

  1. $ dotnet run
  2. 1.234
  3. 1.234
  4. 1.234
  5. System.Single
  6. System.Double
  7. System.Decimal

这是输出。

我们可以使用各种语法来创建浮点值。

Program.cs

  1. using System;
  2. namespace Notations
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. float n1 = 1.234f;
  9. float n2 = 1.2e-3f;
  10. float n3 = (float)1 / 3;
  11. Console.WriteLine(n1);
  12. Console.WriteLine(n2);
  13. Console.WriteLine(n3);
  14. }
  15. }
  16. }

我们有三种创建浮点值的方法。 第一种是使用小数点的“正常”方式。 第二种使用科学计数法。 最后一个是数字运算的结果。

  1. float n2 = 1.2e-3f;

这是浮点数的科学表示法。 也称为指数表示法,它是一种写数字太大或太小而不能方便地用标准十进制表示法写的方式。

  1. float n3 = (float) 1 / 3;

(float)构造称为转换。 默认情况下,除法运算将返回整数。 通过强制转换,我们得到一个浮点数。

  1. $ dotnet run
  2. 1.234
  3. 0.0012
  4. 0.3333333

这是上面程序的输出。

floatdouble类型不精确。

Program.cs

  1. using System;
  2. namespace InExact
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. double n1 = 0.1 + 0.1 + 0.1;
  9. double n2 = 1 / 3.0;
  10. if (n1 == n2)
  11. {
  12. Console.WriteLine("Numbers are equal");
  13. }
  14. else
  15. {
  16. Console.WriteLine("Numbers are not equal");
  17. }
  18. }
  19. }
  20. }

比较浮点值时应格外小心。

  1. $ dotnet run
  2. Numbers are not equal

而且数字不相等。

假设一个短跑运动员跑了 1 个小时,跑了 9.87 秒。 他的公里/小时速度是多少?

Program.cs

  1. using System;
  2. namespace Sprinter
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. float distance = 0.1f;
  9. float time = 9.87f / 3600;
  10. float speed = distance / time;
  11. Console.WriteLine("The average speed of a sprinter is {0} km/h", speed);
  12. }
  13. }
  14. }

在此示例中,必须使用浮点值。

  1. float distance = 0.1f;

100m 是 0.1 km。

  1. float time = 9.87f / 3600;

9.87s 是9.87 / (60 * 60)h。

  1. float speed = distance / time;

为了获得速度,我们将距离除以时间。

  1. $ dotnet run
  2. The average speed of a sprinter is 36.47416 km/h

This is the output of the program.

C# 枚举

枚举类型(也称为枚举或枚举)是由一组命名值组成的数据类型。 可以将任何枚举器分配为已声明为具有枚举类型的变量作为值。 枚举使代码更具可读性。

Program.cs

  1. using System;
  2. namespace Enumerations
  3. {
  4. enum Days
  5. {
  6. Monday,
  7. Tuesday,
  8. Wednesday,
  9. Thursday,
  10. Friday,
  11. Saturday,
  12. Sunday
  13. }
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. Days day = Days.Monday;
  19. if (day == Days.Monday)
  20. {
  21. Console.WriteLine("It is Monday");
  22. }
  23. Console.WriteLine(day);
  24. foreach (int i in Enum.GetValues(typeof(Days)))
  25. {
  26. Console.WriteLine(i);
  27. }
  28. }
  29. }
  30. }

在我们的代码示例中,我们为工作日创建一个枚举。

  1. enum Days
  2. {
  3. Monday,
  4. Tuesday,
  5. Wednesday,
  6. Thursday,
  7. Friday,
  8. Saturday,
  9. Sunday
  10. }

使用enum关键字创建枚举。 星期一,星期二,…星期日实际上存储着数字0..6

  1. Days day = Days.Monday;

我们有一个名为day的变量,其类型为Days。 它被初始化为星期一。

  1. if (day == Days.Monday)
  2. {
  3. Console.WriteLine("It is Monday");
  4. }

与将日变量与某个数字进行比较相比,此代码更具可读性。

  1. Console.WriteLine(day);

该行将在星期一打印到控制台。

  1. foreach (int i in Enum.GetValues(typeof(Days)))
  2. {
  3. Console.WriteLine(i);
  4. }

此循环将0..6打印到控制台。 我们得到enum值的基础类型。 对于计算机,enum只是一个数字。 typeof是用于获取类型的System.Type对象的运算符。 GetValues()方法需要它。 此方法返回指定枚举值的数组。 foreach关键字逐个元素地遍历数组并将其打印到终端。

我们会进一步进行枚举。

Program.cs

  1. using System;
  2. using System;
  3. namespace Seasons
  4. {
  5. public enum Seasons : byte
  6. {
  7. Spring = 1,
  8. Summer = 2,
  9. Autumn = 3,
  10. Winter = 4
  11. }
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. Seasons s1 = Seasons.Spring;
  17. Seasons s2 = Seasons.Autumn;
  18. Console.WriteLine(s1);
  19. Console.WriteLine(s2);
  20. }
  21. }
  22. }

季节可以很容易地用作枚举。 我们可以为enum指定基础类型,并且可以为其提供确切的值。

  1. public enum Seasons : byte
  2. {
  3. Spring = 1,
  4. Summer = 2,
  5. Autumn = 3,
  6. Winter = 4
  7. }

使用冒号和数据类型,我们指定enum的基础类型。 我们还给每个成员一个特定的号码。

  1. Console.WriteLine(s1);
  2. Console.WriteLine(s2);

这两行将enum值打印到控制台。

  1. $ dotnet run
  2. Spring
  3. Autumn

This is the output of the program.

C# 元组

元组是异类数据值的有序的,不变的列表。 元组是值类型。 元组必须至少包含两个元素。 元组用圆括号()定义。

Program.cs

  1. using System;
  2. namespace Tuples
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. var words = ("sky", "blue", "rock", "fountain");
  9. Console.WriteLine(words);
  10. Console.WriteLine(words.Item1);
  11. Console.WriteLine(words.Item2);
  12. var words2 = (w1: "forest", w2: "deep", w3: "sea");
  13. Console.WriteLine(words2.w1);
  14. Console.WriteLine(words2.w2);
  15. Console.WriteLine(words2.w3);
  16. }
  17. }
  18. }

在示例中,我们定义了两个元组。

  1. var words = ("sky", "blue", "rock", "fountain");

这是一个未命名的元组定义。

  1. Console.WriteLine(words);

我们将元组的所有元素打印到控制台。

  1. Console.WriteLine(words.Item1);
  2. Console.WriteLine(words.Item2);

我们打印前两个元素。 我们使用特殊的Item1Item2,…属性访问未命名元组的元素。

  1. var words2 = (w1: "forest", w2: "deep", w3: "sea");

这是一个命名元组的定义。

  1. Console.WriteLine(words2.w1);
  2. Console.WriteLine(words2.w2);
  3. Console.WriteLine(words2.w3);

我们通过元素名称访问元素。

  1. $ dotnet run
  2. (sky, blue, rock, fountain)
  3. sky
  4. blue
  5. forest
  6. deep
  7. sea

This is the output.

C# 字符串和字符

string是代表计算机程序中文本数据的数据类型。 C# 中的字符串是 Unicode 字符序列。 char是单个 Unicode 字符。 字符串用双引号引起来。

由于字符串在每种编程语言中都非常重要,因此我们将为它们专门整整一章。 这里我们仅举一个小例子。

Program.cs

  1. using System;
  2. namespace Strings
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string word = "ZetCode";
  9. char c = word[0];
  10. Console.WriteLine(c);
  11. }
  12. }
  13. }

程序将'Z'字符打印到终端。

  1. string word = "ZetCode";

在这里,我们创建一个字符串变量,并为其分配"ZetCode"值。

  1. char c = word[0];

string是 Unicode 字符数组。 我们可以使用数组访问符号从字符串中获取特定字符。 方括号内的数字是字符数组的索引。 索引从零开始计数。 这意味着第一个字符的索引为 0。

  1. $ dotnet run
  2. Z

该程序将"ZetCode"字符串的第一个字符打印到控制台。

C# 数组

数组是处理元素集合的复杂数据类型。 每个元素都可以通过索引访问。 数组的所有元素必须具有相同的数据类型。

我们将整章专门介绍数组。 这里我们仅显示一个小例子。

Program.cs

  1. using System;
  2. namespace ArrayEx
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. int[] numbers = new int[5];
  9. numbers[0] = 3;
  10. numbers[1] = 2;
  11. numbers[2] = 1;
  12. numbers[3] = 5;
  13. numbers[4] = 6;
  14. int len = numbers.Length;
  15. for (int i = 0; i < len; i++)
  16. {
  17. Console.WriteLine(numbers[i]);
  18. }
  19. }
  20. }
  21. }

在此示例中,我们声明一个数组,用数据填充它,然后将数组的内容打印到控制台。

  1. int[] numbers = new int[5];

我们声明一个整数数组,该数组最多可以存储五个整数。 因此,我们有五个元素组成的数组,索引为0..4

  1. numbers[0] = 3;
  2. numbers[1] = 2;
  3. numbers[2] = 1;
  4. numbers[3] = 5;
  5. numbers[4] = 6;

在这里,我们为创建的数组分配值。 我们可以通过数组访问符号访问数组的元素。 它由数组名称和方括号组成。 在方括号内,我们指定所需元素的索引。

  1. int len = numbers.Length;

每个数组都有一个Length属性,该属性返回数组中的元素数。

  1. for (int i=0; i<len; i++)
  2. {
  3. Console.WriteLine(numbers[i]);
  4. }

我们遍历数组并将数据打印到控制台。

C# DateTime

DateTime是一种值类型。 它代表时间的瞬间,通常表示为日期和时间。

Program.cs

  1. using System;
  2. namespace DateTimeEx
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. DateTime now = DateTime.Now;
  9. System.Console.WriteLine(now);
  10. System.Console.WriteLine(now.ToShortDateString());
  11. System.Console.WriteLine(now.ToShortTimeString());
  12. }
  13. }
  14. }

我们以三种不同的格式显示今天的日期:日期&时间,日期和时间。

  1. DateTime now = DateTime.Now;

获取一个DateTime对象,该对象设置为此计算机上的当前日期和时间,表示为本地时间。

  1. System.Console.WriteLine(now);

此行以完整格式打印日期。

  1. System.Console.WriteLine(now.ToShortDateString());
  2. System.Console.WriteLine(now.ToShortTimeString());

ToShortDateString()返回短日期字符串格式,ToShortTimeString()返回短时间字符串格式。

  1. $ dotnet run
  2. 12/5/2018 8:09:56 PM
  3. 12/5/2018
  4. 8:09 PM

我们看到示例的输出。

C# 类型转换

我们经常一次处理多种数据类型。 将一种数据类型转换为另一种数据类型是编程中的常见工作。 类型转换或类型转换是指将一种数据类型的实体更改为另一种。 有两种转换类型:隐式转换和显式转换。 隐式类型转换,也称为强制转换,是编译器自动进行的类型转换。

Program.cs

  1. using System;
  2. namespace ImplicitTypeConversion
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. int val1 = 0;
  9. byte val2 = 15;
  10. val1 = val2;
  11. Console.WriteLine(val1.GetType());
  12. Console.WriteLine(val2.GetType());
  13. Console.WriteLine(12 + 12.5);
  14. Console.WriteLine("12" + 12);
  15. }
  16. }
  17. }

在此示例中,我们有几个隐式转换。

  1. val1 = val2;

在这里,我们使用两种不同的类型:intbyte。 我们将byte值分配给int值。 这是一个扩大的操作。 int值有四个字节。 字节值只有一个字节。 允许扩展转换。 如果我们想将int分配给byte,这将是缩短转换。 C# 编译器不允许隐式缩短转换。 这是因为在隐式缩短转换中,我们可能会无意间降低精度。 我们可以缩短转换时间,但是我们必须将其告知编译器。 我们知道自己在做什么。 可以通过显式转换来完成。

  1. Console.WriteLine(12 + 12.5);

我们添加两个值:一个整数和一个浮点值。 结果是浮点值。 这是一个不断扩大的隐式转换。

  1. Console.WriteLine("12" + 12);

结果为 1212。将整数转换为字符串,然后将两个字符串连接在一起。

接下来,我们将展示一些在 C# 中的显式转换。

Program.cs

  1. using System;
  2. namespace ExplicitTypeConversion
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. double b = 13.5;
  9. float a = (float) b;
  10. float c = (int) a;
  11. Console.WriteLine(a);
  12. Console.WriteLine(b);
  13. Console.WriteLine(c);
  14. }
  15. }
  16. }

我们有三个值。 我们使用这些值进行一些显式转换。

  1. float a = (float) b;

我们将double值转换为float值。 通过在两个圆括号之间指定所需的类型来进行显式转换。 在这种情况下,不会损失任何精度。 数字 13.5 可以安全地分配给这两种类型。

  1. float c = (int) a;

我们将float值转换为int值。 在此语句中,我们失去了一些精度:13.5 变为 13。

  1. $ dotnet run
  2. 13.5
  3. 13.5
  4. 13

我们看到了程序的输出。

C# 可空类型

不能为值类型分配null字面值,可以给引用类型分配值。 使用数据库的应用处理空值。 因此,C# 语言中引入了特殊的可空类型。 可空类型是System.Nullable<T>结构的实例。

Program.cs

  1. using System;
  2. class NullableType
  3. {
  4. static void Main()
  5. {
  6. Nullable<bool> male = null;
  7. int? age = null;
  8. Console.WriteLine(male.HasValue);
  9. Console.WriteLine(age.HasValue);
  10. }
  11. }

一个简单的示例,演示可空类型。

  1. Nullable<bool> male = null;
  2. int? age = null;

有两种方法可以声明可为空的类型。 在Nullable<T>通用结构中,在尖括号之间指定了类型,或者我们可以在类型后使用问号。 后者实际上是第一种表示法的简写。

  1. $ dotnet run
  2. False
  3. False

这是示例的输出。

C# 转换&解析方法

有两种用于转换值的方法。

Program.cs

  1. using System;
  2. namespace ConvertEx
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine(Convert.ToBoolean(0.3));
  9. Console.WriteLine(Convert.ToBoolean(3));
  10. Console.WriteLine(Convert.ToBoolean(0));
  11. Console.WriteLine(Convert.ToBoolean(-1));
  12. Console.WriteLine(Convert.ToInt32("452"));
  13. Console.WriteLine(Convert.ToInt32(34.5));
  14. }
  15. }
  16. }

Convert类具有许多用于转换值的方法。 我们使用其中两个。

Console.WriteLine(Convert.ToBoolean(0.3));

我们将double值转换为bool值。

Console.WriteLine(Convert.ToInt32("452"));

在这里,我们将string转换为int

$ dotnet run
True
True
False
True
452
34

This is the output.

Program.cs

using System;

namespace ParseEx
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Int32.Parse("34"));
            Console.WriteLine(Int32.Parse("-34"));
            Console.WriteLine(Int32.Parse("+34"));
        }
    }
}

将字符串转换为整数是非常常见的任务。 当我们从数据库或 GUI 组件中获取值时,我们通常会进行此类转换。

Console.WriteLine(Int32.Parse("34"));

我们使用Int32类的Parse()方法将string转换为int值。

$ dotnet run
34
-34
34

This is the output.

在 C# 教程的这一部分中,我们介绍了数据类型及其转换。