C#中的数字字面量后缀及其作用 | Numeric Literal Suffixes and Their Usage in C#
在C#编程中,我们经常需要使用不同类型的数字,如整数、浮点数和高精度数字等。为了方便表示这些数字并明确其数据类型,C#提供了各种数字字面量后缀。本文将通过实例详细介绍这些后缀的作用和用法。
When programming in C#, we often need to use different types of numbers, such as integers, floating-point numbers, and high-precision numbers. To facilitate the representation of these numbers and clarify their data types, C# provides various numeric literal suffixes. This article will introduce the usage and effects of these suffixes in detail through examples.
1. m或M后缀表示decimal类型 | m or M Suffix for decimal Type
decimal
类型用于表示高精度的十进制
数字。在C#中,我们可以使用m
或M
后缀来指定一个字面量为decimal
类型。
The decimal
type is used to represent high-precision decimal numbers. In C#, we can use the m
or M
suffix to specify a literal as the decimal
type.
decimal price = 9.99m;
decimal tax = 1.50M;
decimal total = price + tax;
Console.WriteLine($"Total: {total}"); // 输出: Total: 11.49
2. f或F后缀表示float类型 | f or F Suffix for float Type
float
类型用于表示单精度
浮点数。通过在数字字面量末尾添加f
或F
后缀,我们可以将其指定为float
类型。
The float
type is used to represent single-precision floating-point numbers. By appending the f
or F
suffix to the end of a numeric literal, we can specify it as the float
type.
float pi = 3.14159f;
float radius = 2.5F;
float area = pi * radius * radius;
Console.WriteLine($"Area: {area}"); // 输出: Area: 19.63495
3. d或D后缀表示double类型 | d or D Suffix for double Type
double
类型用于表示双精度
浮点数。如果没有指定后缀,数字字面量默认为double
类型。我们也可以显式地使用d
或D
后缀。
The double
type is used to represent double-precision floating-point numbers. If no suffix is specified, the numeric literal defaults to the double
type. We can also explicitly use the d
or D
suffix.
double distance = 1000.0;
double time = 3600d;
double speed = distance / time;
Console.WriteLine($"Speed: {speed} m/s"); // 输出: Speed: 0.277777777777778 m/s
4. u或U后缀表示uint类型 | u or U Suffix for uint Type
uint
类型表示32位无符号
整数。通过在整数字面量末尾添加u
或U
后缀,我们可以将其指定为uint
类型。
The uint
type represents a 32-bit unsigned integer. By appending the u
or U
suffix to the end of an integer literal, we can specify it as the uint
type.
uint age = 25u;
uint max = uint.MaxValue;
Console.WriteLine($"Age: {age}, Max: {max}"); // 输出: Age: 25, Max: 4294967295
5. l或L后缀表示long类型 | l or L Suffix for long Type
long
类型表示64位有符号
整数。通过在整数字面量末尾添加l
或L
后缀,我们可以将其指定为long
类型。
The long
type represents a 64-bit signed integer. By appending the l
or L
suffix to the end of an integer literal, we can specify it as the long
type.
long population = 7800000000L;
long milliseconds = 1000L * 60 * 60 * 24;
Console.WriteLine($"Population: {population}, Milliseconds in a day: {milliseconds}");
// 输出: Population: 7800000000, Milliseconds in a day: 86400000
6. ul、uL、Ul、UL、lu、Lu、lU或LU后缀表示ulong类型 | ul, uL, Ul, UL, lu, Lu, lU, or LU Suffix for ulong Type
ulong
类型表示64位无符号
整数。通过在整数字面量末尾添加ul
、uL
、Ul
、UL
、lu
、Lu
、lU
或LU
后缀,我们可以将其指定为ulong
类型。
The ulong
type represents a 64-bit unsigned integer. By appending the ul
, uL
, Ul
, UL
, lu
, Lu
, lU
, or LU
suffix to the end of an integer literal, we can specify it as the ulong
type.
ulong bytesInTB = 1024uL * 1024 * 1024 * 1024;
ulong maxValue = ulong.MaxValue;
Console.WriteLine($"Bytes in a TB: {bytesInTB}, Max Value: {maxValue}");
// 输出: Bytes in a TB: 1099511627776, Max Value: 18446744073709551615
在编写C#代码时,合理使用数字字面量后缀可以让编译器正确推断数据类型,减少隐式类型转换带来的潜在问题。同时,明确指定数据类型也有助于提高代码的自文档化程度,使其更易于理解和维护。文章来源:https://www.toymoban.com/news/detail-846866.html
When writing C# code, proper use of numeric literal suffixes allows the compiler to correctly infer data types and reduces potential issues caused by implicit type conversions. Meanwhile, explicitly specifying data types also helps improve the self-documentation of the code, making it easier to understand and maintain.文章来源地址https://www.toymoban.com/news/detail-846866.html
到了这里,关于【C#】数字后缀及其作用 | Numeric Literal Suffixes and Their Usage in C#的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!