0

Why do we have the L at that end here, I understand it's a literal value, but if the data type is long anyway, then is it needed?

public const long l = 100000000L;

Would this produce anything different?

public const long l = 100000000;
FCin
  • 3,804
  • 4
  • 20
  • 49
Cueball
  • 11

4 Answers4

1

2.4.4.2 of the spec states:

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.

• If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong.

• If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong.

• If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.

As such, the L is being inferred ("If the literal has no suffix"), so it is not required. Your two examples will do the exact same thing (once compiled).

Why then might you use L? Well, an example might be:

public const long doesNotCompile = 3000000000 * 2;
public const long doesCompile = 3000000000L * 2;

In the first line, the literal (i.e. 3000000000) is an uint (due to 2.4.4.2) while in the second line it is a long (again due to 2.4.4.2 - since it is explicitly specified).

This means the compiler will allow doesCompile to compile but won't allow doesNotCompile to compile. This is because 3000000000 * 2 is too large to fit inside an uint.

But for basic usages, like yours, where it is a straight assignment - then yes it is not needed in any way. And there are no downsides of not including it.

Community
  • 1
  • 1
mjwills
  • 23,389
  • 6
  • 40
  • 63
0

Both will output the same IL: a call to the int64 constructor: int64(100000000).

InBetween
  • 32,319
  • 3
  • 50
  • 90
-1

It is explained here. Probably the main purpose is to distinguish long and int parameter of overloaded methods.

-1

For as far as I'm aware, it mostly changes the amount of memory allocated by default.

It also makes it so when using implicit type declaration, that the compiler knows what type of value reference you want to use. (which might be important for your application)

enter image description here

enter image description here