If you create a function, which should make the use of typeconversions more easy, but it maybe leads to more errors, would implement it globally, and what may speaks against it - when the readability seems to have improved?

For (a simple) example:

public static T EnforceConversion<T>(this object Result)
    return (T)System.Convert.ChangeType(Result, typeof(T));

This is the most plain example I could dig up. The resultion notation is IMHO much more easy to read then the longterm syntax, which needs a specific type put into the method.

You could call it like:


Which I think has it own merits.

Another programmer might end up with the idea, to put into types which can't be more or less implictly casted, would you risk such consequences for your code? The error display will more or less be as handable as it would be in the original call.


FileInfo MyFile = FileUtilites.SelectFile();
  • 1
    "more easy to read than the long-term syntax" What syntax are you talking about? I would just use bool.Parse("True"). – Dan Wilson Oct 4 '18 at 13:55
  • hi, just in general syntaxwise. for sure is bool parse perfectly valid too, but changetype can be used for any IConvertible which makes it more versatile in my eyes without much coding effort. – mayen Oct 4 '18 at 15:15
  • 1
    in my eyes without much coding effort; what effort are you talking about, you are only saving me (T)System.Convert.ChangeType(Result, typeof(T)); but now I have to learn about your API (EnforceConversion). If I am on your team, I have to hunt this method and then learn it and use it to just save what? I do not see your point so perhaps you need to elaborate or I may have answered your question. – CodingYoshi Oct 5 '18 at 0:35

There two reasons why you would want to avoid this.

First, experienced programmers who already know the long syntax will need to learn a new abstraction. While in itself, it isn't a big deal, it is always easier for a newcommers in a project to feel at home with code respecting well-known standards.

Second, you add a dependency in your code. Basically, everywhere you will need a type conversion, you will need to import or rewrite your function.

Even if the code is a bit shorter and have a bit more explicit naming, maintaining this standard throughout the project is likely to cost more than it will benefit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.