String casts in .NET

后端 未结 5 1355
醉话见心
醉话见心 2021-02-15 02:30

Why is there so may ways to convert to a string in .net? The ways I have seen are .ToString, Convert.ToString() and (string). What is the Difference.

5条回答
  •  情话喂你
    2021-02-15 03:10

    object.ToString() is the most basic way of retrieving a string representation of an object, and can be specifically implemented by the object.

    Convert.ToString() expands on that and provides some specific overloads for primitive types (char, byte, int, double, etc.) that allow for some more type-specific functionality (like base conversion, for example)

    (string) is a casting operator, and will only work if the type is either a string or has an implicit or explicit operator that can convert it to a string. Otherwise you'll get an InvalidCastException

提交回复
热议问题