简体   繁体   English

将 int 转换为字符串和 C# 中的 ToString() 方法有什么区别

[英]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?将 Int 转换为字符串和 ToString() 方法有什么区别?

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.好吧, ToString()只是一个返回字符串的方法调用。 It's defined in object so it's always valid to call on anything (other than a null reference).它在object中定义,因此调用任何东西(null 参考除外)总是有效的。

The cast operator can do one of four things:演员表操作员可以做以下四件事之一:

  • A predefined conversion, eg int to byte预定义的转换,例如intbyte
  • An execution time reference conversion which may fail, eg casting object to string , which checks for the target object being an appropriate type可能会失败的执行时间引用转换,例如将objectstring ,它检查目标 object 是否是适当的类型
  • A user-defined conversion (basically calling a static method with a special name) which is known at compile-time编译时已知的用户定义转换(基本上调用具有特殊名称的 static 方法)
  • An unboxing conversion which may fail, eg casting object to int可能会失败的拆箱转换,例如将objectint

In this case, you're asking the compiler to emit code to convert from int to string .在这种情况下,您要求编译器发出代码以从int转换为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.不同之处在于,使用强制转换,您要求编译器假设 int 实际上是一个字符串,但事实并非如此。

With the ToString(), you ask for a string representation for the int, which is in fact a string:)使用 ToString(),您要求 int 的字符串表示形式,它实际上是一个字符串:)

Um, ToString() is calling a method that returns a string representation of the integer.嗯,ToString() 正在调用一个方法,该方法返回 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.当你转换时,你没有返回一个表示,你是说你想引用相同的 object (好吧,在这种情况下是值类型),但你想引用它作为不同的类型。

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.仅当您要转换的类型(目标类型)与要转换的 object 的类型相同,或者目标类型是转换 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.实际上可以在提供源或目标类型声明隐式或显式转换的强制转换中进行转换,但 Int32 类型不会对 String 目标类型执行此操作。

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. .ToString() 方法是在 System.Object 类型(所有 .NET 类型从中派生)上实现的方法,并且可以在特定派生类型上覆盖。

Therefore the "int" type has it's own.ToString() method which knows all about ints, and how to convert them to a string representation.因此,“int”类型有它自己的.ToString() 方法,该方法了解整数的所有信息,以及如何将它们转换为字符串表示形式。

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).使用 (string)myint 显式转换,您要求编译器强制将一种类型转换/转换为另一种类型(在这种情况下,将 int 转换为字符串)。 It fails because the compiler says that a string and an int are incompatible types.它失败了,因为编译器说 string 和 int 是不兼容的类型。

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!因此,显式转换失败,因为编译器说 int不是字符串,但是 .ToString() 调用成功,因为 int 类型说它的值可以表示为字符串,并且确实如此!

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. ToString()方法是编程和许多其他语言中最有用的方法之一,例如 Java,在Object级别实现完全相同的方法。

You can define your own and the signature in C# must always be this:您可以定义自己的签名,并且 C# 中的签名必须始终是这样的:

public override string ToString()

Which means this method will override the one define in the Object-class and returns a string.这意味着此方法将覆盖 Object 类中的定义并返回一个字符串。 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 .此外,您可以在 integer 上使用 ToString 的具体原因是因为在 C# 中,整数都是 Struct Int32的实例。
Seeing as Int32 is at the same level of a class, it can have its own methods, one of which is ToString().由于Int32与 class 处于同一级别,因此它可以有自己的方法,其中之一是 ToString()。

One more thing never use.ToString() if you think the object might be null or your application will crach example:如果您认为 object 可能是 null 或者您的应用程序将出现示例,那么永远不要使用另一件事。

//Emulates the problem that might happen //模拟可能发生的问题
object obj = null; object obj = null; obj.ToString(); obj.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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