hfrmobile
hfrmobile

Reputation: 1370

Why does System.Convert() provide implementation of not supported conversions?

MS Documentation:

ToDateTime(Object)
Converts the value of the specified object to a DateTime object.

ToDateTime(Double)
Calling this method always throws InvalidCastException.

ToDateTime(Int32)
Calling this method always throws InvalidCastException.

ToDateTime(Int16)
Calling this method always throws InvalidCastException.

ToDateTime(Int64)
Calling this method always throws InvalidCastException.

ToDateTime(Decimal) Calling this method always throws InvalidCastException.

Why providing such methods?

MS Source code (including comments):

https://referencesource.microsoft.com/#mscorlib/system/convert.cs

At the beginning is a matrix describing which conversions are supported and which are not:

    // From:  To: Bol Chr SBy Byt I16 U16 I32 U32 I64 U64 Sgl Dbl Dec Dat Str
    // ----------------------------------------------------------------------
    // Boolean     x       x   x   x   x   x   x   x   x   x   x   x       x
    // Char            x   x   x   x   x   x   x   x   x                   x
    // SByte       x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // Byte        x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // Int16       x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // UInt16      x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // Int32       x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // UInt32      x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // Int64       x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // UInt64      x   x   x   x   x   x   x   x   x   x   x   x   x       x
    // Single      x       x   x   x   x   x   x   x   x   x   x   x       x
    // Double      x       x   x   x   x   x   x   x   x   x   x   x       x
    // Decimal     x       x   x   x   x   x   x   x   x   x   x   x       x
    // DateTime                                                        x   x
    // String      x   x   x   x   x   x   x   x   x   x   x   x   x   x   x
    // ----------------------------------------------------------------------

Each 'x' stands for a supported conversion. But for the blanks " " (not supported versions) there are methods too, e.g.

        public static DateTime ToDateTime(byte value) {
            return ((IConvertible)value).ToDateTime(null);
        }

See also similar question: Why does System.Convert has ToDateTime that accepts DateTime?

Upvotes: 6

Views: 134

Answers (1)

Richard
Richard

Reputation: 108975

Because any other option – given user defined (value) types – is more complicated. This is even more so when there is no generics (as was the case when these types were defined).

Compare .NET 7/C# 11 with static members of (generic) interfaces like IParsable<TSelf> potentially will still throw (or fail) at runtime because anything can be passed when using methods that take an Object.

Alternately you could force all possible data types to only be defined at compile time, which is fine until you need to pass data to/from other programs and users which are imperfect.

Upvotes: 3

Related Questions