System.Convert has a really useful utility for converting datatypes from one type to another. In my project, I have many custom types. I want to convert command line arguments to these custom types (some of which are quite complex). It would be nice if these existed within System.Convert so I could just do something like this:
No, you can't add them to the
Convert class - I would suggest adding conversion methods to your actual types, such as:
and instance methods going the other way:
int x = myCustomType.ToInt32();
(Static factory methods are often better than adding lots of overloaded constructors, IMO. They allows various alternatives - including returning a null value where appropriate, or caching - and can make the calling code clearer.)
I would also strongly recommend that you don't go overboard on the number of conversions you supply. Not many custom types really have a single natural conversion from all kinds of primitive types.