Yes it's a bit inconsistent, but it's taken pretty much directly from the Microsoft documentation and I wanted to be sure everyone picked up on it in case it appeared on the exam.
The decimal --> double conversion may not be a great example because you're right a cast is required.
Personally, if I had been on the Microsoft team, I would have argued that loss of precision makes a narrowing conversion. Saying it's not is like saying, "Casting an Employee into a Person is a widening conversion. Sure you lose the EmployeeId but you keep the name."
From
Type Conversion in the .NET Framework:
Quote:
Widening conversions cannot result in data loss (although they may result in a loss of precision). Because data cannot be lost, compilers can handle the conversion implicitly or transparently, without requiring the use of an explicit conversion method or a casting operator.
|
And on
Type Conversion Tables there's a table that lists decimal --> double and decimal --> double as widening conversions.
So the documentation is a bit inconsistent. Microsoft regards those conversions as widening but the compiler requires a cast.
Here's an example that doesn't require a cast:
Code:
long longValue = 1234567890;
float floatValue = longValue;
Console.WriteLine(string.Format("{0,15:N0}", longValue));
Console.WriteLine(string.Format("{0,15:N0}", floatValue));
The result is:
Code:
1,234,567,890
1,234,568,000
I think as long as you understand the issues, you'll be okay on the exam. To summarize, the issues are:
- Widening conversions may result in a loss of precision but not magnitude.
- In general (except for this case converting from decimal), widening conversions don't require casting.
I hope that helps.