Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 5
    My problem with this is that you lose type safety. myInt could be anything. Nothing here says take an integer and convert it to a string. myInt could be an object and an object can't be converted to a string. That's known at compile time, but it wouldn't even raise a runtime exception it would just allow bad data. Commented Dec 15, 2016 at 16:29
  • 5
    @TimothyGonzalez That's an edge case, if you call .ToString() its usually because you need it to be a string and it can be a string. Commented Feb 15, 2017 at 13:58
  • 57
    @nfgallimore It is not a valid edge case it is not even an edge case in this situation. The OP stated he had an int type he wanted converted to a string. If it is an int type, then plain and simple the value is an int. The compiler will insure that is the case. Not even sure how you went off on that tangent. The OP didn't ask how to make sure a random reference was an integer and then convert it to a string. Perhaps in that case you have a point, but that is not the case. Commented Jun 22, 2018 at 23:46
  • 2
    @MehdiDehghani, data type int from the question would not be null. But for the sake of argument, data type int? could be null, but that would return an empty string, not throw. However, should the data type not be int, but instead be an int? boxed to object, then yes, it would throw if null. Commented Nov 30, 2018 at 21:37
  • 42
    The comments on this answer are ridiculous. Commented Mar 22, 2019 at 15:16