为什么.NET自然会假设某些类型?

时间:2021-09-28 15:28:17

Consider the following code:

请考虑以下代码:

var x = 32;

The number 32 will fit in within sbyte, byte, short, ushort, etc.

数字32将适合sbyte,byte,short,ushort等。

Why does .NET assume this is an int?

为什么.NET假设这是一个int?

Same question for var x = 3.2; .NET assumes double

var x = 3.2的相同问题; .NET假定为double

1 个解决方案

#1


4  

Why does .NET assume this is an int?

为什么.NET假设这是一个int?

Wrong question/subject. It is the C# compiler that assume this is an int.

错误的问题/主题。 C#编译器假设这是一个int。

Taken from 2.4.4.2 Integer literals:

取自2.4.4.2整数文字:

The type of an integer literal is determined as follows:
If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.

整数文字的类型确定如下:如果文字没有后缀,则它具有这些类型中的第一个,其值可以表示为:int,uint,long,ulong。

and from 2.4.4.3 Real literals:

从2.4.4.3真正的文字:

If no real type suffix is specified, the type of the real literal is double.

如果未指定实际类型后缀,则实数的类型为double。

Another important "trick" of the compiler, that makes this legal:

编译器的另一个重要“技巧”,使这合法:

byte b = 5;

(normally 5 would be an int, and there is no implicit conversion from int to byte), but:

(通常5将是一个int,并且没有从int到byte的隐式转换),但是:

Taken from 6.1.6 Implicit constant expression conversions:

取自6.1.6隐式常量表达式转换:

An implicit constant expression conversion permits the following conversions:

隐式常量表达式转换允许以下转换:

  • A constant-expression (Section 7.15) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.

    如果constant-expression的值在目标类型的范围内,则int类型的常量表达式(第7.15节)可以转换为sbyte,byte,short,ushort,uint或ulong类型。

  • A constant-expression of type long can be converted to type ulong, provided the value of the constant-expression is not negative.

    如果constant-expression的值不是负数,则long类型的常量表达式可以转换为ulong类型。

#1


4  

Why does .NET assume this is an int?

为什么.NET假设这是一个int?

Wrong question/subject. It is the C# compiler that assume this is an int.

错误的问题/主题。 C#编译器假设这是一个int。

Taken from 2.4.4.2 Integer literals:

取自2.4.4.2整数文字:

The type of an integer literal is determined as follows:
If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.

整数文字的类型确定如下:如果文字没有后缀,则它具有这些类型中的第一个,其值可以表示为:int,uint,long,ulong。

and from 2.4.4.3 Real literals:

从2.4.4.3真正的文字:

If no real type suffix is specified, the type of the real literal is double.

如果未指定实际类型后缀,则实数的类型为double。

Another important "trick" of the compiler, that makes this legal:

编译器的另一个重要“技巧”,使这合法:

byte b = 5;

(normally 5 would be an int, and there is no implicit conversion from int to byte), but:

(通常5将是一个int,并且没有从int到byte的隐式转换),但是:

Taken from 6.1.6 Implicit constant expression conversions:

取自6.1.6隐式常量表达式转换:

An implicit constant expression conversion permits the following conversions:

隐式常量表达式转换允许以下转换:

  • A constant-expression (Section 7.15) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.

    如果constant-expression的值在目标类型的范围内,则int类型的常量表达式(第7.15节)可以转换为sbyte,byte,short,ushort,uint或ulong类型。

  • A constant-expression of type long can be converted to type ulong, provided the value of the constant-expression is not negative.

    如果constant-expression的值不是负数,则long类型的常量表达式可以转换为ulong类型。