C# - 数据类型

本文关键字:数据类型 | 更新日期: 2023-09-12 17:38:01

C# 是一种强类型语言。这意味着我们必须声明变量的类型,该变量指示它将要存储的值类型,例如整数、浮点数、小数、文本等。

下面声明和初始化了不同数据类型的变量。

string stringVar = "Hello World!!";
int intVar = 100;
float floatVar = 10.2f;
char charVar = 'A';
bool boolVar = true;

C# 主要将数据类型分为两种类型:值类型和引用类型。值类型包括简单类型(如 int、float、bool 和 char)、枚举类型、结构类型和可为 Null 的值类型。引用类型包括类类型、接口类型、委托类型和数组类型。在下一章中详细了解值类型和引用类型

MM_0

C#

C# 中的预定义数据类型

包括一些预定义的值类型和引用类型。下表列出了预定义的数据类型:

Type Description Range Suffix
byte 8-bit unsigned integer 0 to 255
sbyte 8-bit signed integer -128 to 127
short 16-bit signed integer -32,768 to 32,767
ushort 16-bit unsigned integer 0 to 65,535
int 32-bit signed integer -2,147,483,648
to
2,147,483,647
uint 32-bit unsigned integer 0 to 4,294,967,295 u
long 64-bit signed integer -9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
l
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 ul
float 32-bit Single-precision floating point type -3.402823e38 to 3.402823e38 f
double 64-bit double-precision floating point type -1.79769313486232e308 to 1.79769313486232e308 d
decimal 128-bit decimal type for financial and monetary calculations (+ or -)1.0 x 10e-28
to
7.9 x 10e28
m
char 16-bit single Unicode character Any valid character, e.g. a,*, x0058 (hex), oru0058 (Unicode)
bool 8-bit logical true/false value True or False
object Base type of all other types.
string A sequence of Unicode characters
DateTime Represents date and time 0:00:00am 1/1/01
to
11:59:59pm 12/31/9999

如上表所示,每种数据类型(字符串和对象除外)都包含值范围。如果值超出数据类型的允许范围,编译器将给出错误。例如,int 数据类型的范围是 -2,147,483,648 到 2,147,483,647。因此,如果您分配的值不在此范围内,则编译器将给出错误。

// compile time error: Cannot implicitly convert type 'long' to 'int'.
int i = 21474836470; 

无符号整数、长整型、浮点型、双精度型和十进制型的值必须分别以 u、l、f、d 和 m 为后缀。

uint ui = 100u;
float fl = 10.2f;
long l = 45755452222222l;
ulong ul = 45755452222222ul;
double d = 11452222.555d;
decimal mon = 1000.15m;

别名与 .NET 类型

预定义的数据类型是其 .NET 类型(CLR 类)名称的别名。下表列出了预定义数据类型的别名和相关 .NET 类名。

Alias .NET Type Type
byte System.Byte struct
sbyte System.SByte struct
int System.Int32 struct
uint System.UInt32 struct
short System.Int16 struct
ushort System.UInt16 struct
long System.Int64 struct
ulong System.UInt64 struct
float System.Single struct
double System.Double struct
char System.Char struct
bool System.Boolean struct
object System.Object Class
string System.String Class
decimal System.Decimal struct
DateTime System.DateTime struct

这意味着无论您定义变量 int 还是 Int32 变量,两者都是相同的。

int i = 345;
Int32 i = 345;// same as above 

默认值

每种数据类型都有一个默认值。数值类型为 0,布尔值为 false,字符为 '' 作为默认值。使用 default(typename) 分配数据类型或 C# 7.1 及更高版本的默认值,请使用 default 文本。

int i = default(int); // 0
float f = default(float);// 0
decimal d = default(decimal);// 0
bool b = default(bool);// false
char c = default(char);// ''
// C# 7.1 onwards
int i = default; // 0
float f = default;// 0
decimal d = default;// 0
bool b = default;// false
char c = default;// ''

转换

在 C# 中,某些数据类型的值会自动转换为不同的数据类型。这称为隐式转换。

int i = 345;
float f = i;
Console.WriteLine(f); //output: 345

在上面的示例中,整数变量的值i分配给浮点类型f的变量,因为此转换操作是在 C# 中预定义的。

下面是一个隐式数据类型转换表。

Implicit Conversion From To
sbyte short, int, long, float, double, decimal
byte short, ushort, int, uint, long, ulong, float, double, decimal
short int, long, float, double, or decimal
ushort int, uint, long, ulong, float, double, or decimal
int long, float, double, or decimal.
uint long, ulong, float, double, or decimal
long float, double, or decimal
ulong float, double, or decimal
char ushort, int, uint, long, ulong, float, double, or decimal
float Double

long 或 ulong 到 float 以及从 long 或 ulong 到 double 的双重转换可能会导致精度损失。没有数据类型隐式转换为 char 类型。

但是,并非所有数据类型都隐式转换为其他数据类型。例如,int 类型不能隐式转换为 uint。必须显式指定它,如下所示。

public static void Main()
{
    int i = 100;
    uint u = (uint) i;
    Console.Write(i);
}

在上面的示例中,通过在括号 (uint) 中指定 uint,整数i显式转换为 uint。这会将整数转换为 uint。