What's the difference between casting an int to a string and the ToString() method in C#

后端 未结 6 1705
北海茫月
北海茫月 2020-12-01 17:49

What\'s the difference between casting an Int to a string and the ToString() method ?

For example :-

int MyInt = 10;
label1.Text = (string)MyInt;             


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 18:16

    The difference is that with the cast, you ask the compiler to assume that the int is in fact a string, which is not the case.

    With the ToString(), you ask for a string representation for the int, which is in fact a string :)

提交回复
热议问题