What’s the difference between casting an Int to a string and the ToString() method ?
For example :-
int MyInt = 10; label1.Text = (string)MyInt; // This Doesn't Work label1.Text = MyInt.ToString(); // but this does.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well,
ToString()is just a method call which returns a string. It’s defined inobjectso it’s always valid to call on anything (other than a null reference).The cast operator can do one of four things:
inttobyteobjecttostring, which checks for the target object being an appropriate typeobjecttointIn this case, you’re asking the compiler to emit code to convert from
inttostring. None of the above options apply, so you get a compile-time error.