简体   繁体   中英

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

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.

Well, ToString() is just a method call which returns a string. It's defined in object so it's always valid to call on anything (other than a null reference).

The cast operator can do one of four things:

  • A predefined conversion, eg int to byte
  • An execution time reference conversion which may fail, eg casting object to string , which checks for the target object being an appropriate type
  • A user-defined conversion (basically calling a static method with a special name) which is known at compile-time
  • An unboxing conversion which may fail, eg casting object to int

In this case, you're asking the compiler to emit code to convert from int to string . None of the above options apply, so you get a compile-time error.

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:)

Um, ToString() is calling a method that returns a string representation of the integer.

When you cast, you are not returning a representation, you are saying that you want to reference the same object (well, value-type in this case) but you want to reference it as a different type.

A cast will only succeed if the type you are casting to (target type) is the same type as the object being cast or the target type is a superclass or interface of the cast object.

It is actually possible to do conversion in a cast providing the the source or target type declare implicit or explicit conversions, but the Int32 type does not do this for the String target type.

The.ToString() method is a method implemented on the System.Object type (from which all .NET types derive) and can be overridden on specific derived types.

Therefore the "int" type has it's own.ToString() method which knows all about ints, and how to convert them to a string representation.

With the (string)myint explicit cast, you are asking the compiler to forcefully convert/cast one type to another (in this case, an int into a string). It fails because the compiler says that a string and an int are incompatible types.

So, the explicit cast fails because the compiler says that an int is not a string, however the.ToString() call succeeds because the int type says that it's value can be represented as a string, and does so!

The ToString() method is one of the most usefull methods in programming and many other languages, such as Java, have the exact same method implemented at the Object level.

You can define your own and the signature in C# must always be this:

public override string ToString()

Which means this method will override the one define in the Object-class and returns a string. Inside, you manipulate your string in whichever way you want and then return the result.

Furthermore, the specific reason you can use ToString on that integer is because within C#, integers are all instances of the Struct Int32 .
Seeing as Int32 is at the same level of a class, it can have its own methods, one of which is ToString().

One more thing never use.ToString() if you think the object might be null or your application will crach example:

//Emulates the problem that might happen
object obj = null; obj.ToString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM