I am writing my code in C # and .NET Core 2.0.5. When using TypeConverter for a ushort type and with a conversion error, the FormatException message refers to Int16, not UInt16. Can anyone explain this to me?
Other types that I tested with (decimal, double, float, int, long, short, uint, ulong) return the expected type name in the error message.
To show your point, here is a unittest that will fail. The error message states that "badvalue is not a valid value for Int16."
[Fact] public void FailingToConvertUShort_GivesFormatExceptionMessage_WithCorrectType() { // Arrange var badvalue = "badvalue"; var typeConverter = TypeDescriptor.GetConverter(typeof(ushort)); try { // Act var result = typeConverter.ConvertFrom(context: null, culture: new CultureInfo("en-GB"), value: badvalue); } catch (Exception ex) { // Assert Assert.Equal($"badvalue is not a valid value for {typeof(ushort).Name}.", ex.Message); } }
This is the test result:
Expected: ··· t is a valid value for UInt16.
Actual: ··· t is a valid value for Int16.
source share