Reputation: 6396
I know that something like 1.5
is a double, so you need to use the m
suffix to assign it to a decimal.
decimal d = 1.5; // won't compile
decimal d = 1.5m; // will compile
Now, a literal like 0
is an int. But why can I just assign it to a long? Is there any difference between assigning 0
versus 0L
to a variable of type long?
long l = 0;
long l = 0L;
Upvotes: 3
Views: 167
Reputation: 23983
But why can I just assign it to a long?
11.2.3 of the spec states:
Implicit numeric conversions
The implicit numeric conversions are:
• From sbyte to short, int, long, float, double, or decimal.
• From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.
• From short to int, long, float, double, or decimal.
• From ushort to int, uint, long, ulong, float, double, or decimal.
• From int to long, float, double, or decimal.
• From uint to long, ulong, float, double, or decimal.
• From long to float, double, or decimal.
• From ulong to float, double, or decimal.
• From char to ushort, int, uint, long, ulong, float, double, or decimal.
• From float to double.
Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.
From int to long, float, double, or decimal.
is the relevant one here. 0 is int
by default, so it can be implicitly converted to long
.
Is there any difference between assigning 0 versus 0L to a variable of type long?
They will behave the exact same. The IL is the exact same.
Upvotes: 3