Why does array initialization always apply to int?

Reading All possible C # array initialization syntaxes I wondered why C # always displays an array int/Int32where there is a smaller data type, such as byteor shortwould be enough.

new[] { 30, 130, 230 } // sbyte[] suffices but becomes int[]
new[] { -1, 125, -119 } // sbyte[] suffices  but becomes int[]
new[] { -31647, -1337, 23456} // short suffices but becomes int[]

In the question mentioned, Eric Lippert claims that the “best type” is used - see below, but how is the intbest type possible? If we are going to outwit, why not use long, then?

The type of an array element is inferred by calculating the best type, if there is one, from all given elements that have types. All elements must be implicitly convertible to this type.

I would suspect that processing 8 or 16-bit data types may be faster than 32-bit structures, for example. when using SIMD, where four instances bytecan fit into the register space of one int/Int32. I know that SSE instructions are not (widely) used by the JIT compiler , but this use inteverywhere "ensures that this does not help when the JIT Compiler includes such optimizations.

Can someone elaborate on these facts and tell why he always resorts to int?

// Edit // I don’t care about the specification, which prescribes that a literal without a prefix should be considered int. To rephrase the question:

, ? ? , - (SIMD).

+4
5

, ?

-, , , , . -, int, .

?

. , . , , , , . # .

, - (SIMD).

, # " SIMD-" " " #. . , , . , , .

# , , , , , . -, columnWidths = new [] { 10, 20, 30 }; .

+6

# 5.0 spec 2.4.4.2

• , , : int, uint, long, ulong.

• U u, , : uint, ulong.

• L l, , : long, ulong.

• UL, Ul, uL, ul, LU, Lu, lU lu, ulong.

... int.

. var i = 10; int.

+4

, , System.Int32, (, System.Int16), System.Int32.

System.Int32, System.Int32[].

, , ( , ) " , ", #.

V5.0 - # ( VS2013) 2.4.4.2:

int, uint, long ulong.

.. byte, sbyte, short unsigned short .

+1

- 30, 130, 230, int32;

new[] { 30, 130, 230 }; // <- array of int's

, :

  new byte[] { 30, 130, 230 }; // <- treat each value as byte
+1

, , int 32- , , .

, 64- int64 , int .

-2

Source: https://habr.com/ru/post/1529409/


All Articles