.NET中的字符串强制转换 [英] String casts in .NET

查看:125
本文介绍了.NET中的字符串强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会有这样的方法可以在.net中转换为字符串?我见过的方法是.ToString,Convert.ToString()和(string)。有什么不同。

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.

推荐答案

Convert.ToString(obj)



将指定的值转换为其等效的String表示形式。如果指定值为 null ,将返回 String.Empty

返回表示当前Object的字符串。此方法返回对文化敏感的人类可读字符串。例如,对于Double类的值为零的实例,Double.ToString的实现可能会返回 0.00或 0,00,具体取决于当前的UI文化。默认实现返回对象类型的全限定名。

Returns a String that represents the current Object. This method returns a human-readable string that is culture-sensitive. For example, for an instance of the Double class whose value is zero, the implementation of Double.ToString might return "0.00" or "0,00" depending on the current UI culture. The default implementation returns the fully qualified name of the type of the Object.

可以在派生类中重写此方法,以返回对该类型有意义的值。例如,基本数据类型(例如Int32)实现ToString,以便它返回对象表示的值的字符串形式。与ToString相比,需要对字符串格式进行更多控制的派生类必须实现IFormattable,该类的ToString方法使用当前线程的CurrentCulture属性。

This method can be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as Int32, implement ToString so that it returns the string form of the value that the object represents. Derived classes that require more control over the formatting of strings than ToString provides must implement IFormattable, whose ToString method uses the current thread's CurrentCulture property.

这是演员操作,而不是函数调用。如果您确定该对象的类型为字符串,或者该对象具有可将其转换为字符串的隐式或显式运算符,请使用该对象。如果对象为 null并且类型为String或实现自定义强制转换为字符串运算符的类型,则将返回 null

It's a cast operation, not a function call. Use it if you're sure that the object is of type string OR it has an implicit or explicit operator that can convert it to a string. Will return null if the object is null AND of type String or of type which implements custom cast to string operator. See examples.

安全投放操作。与上面相同,但是如果强制转换操作失败,它不会返回异常,而是返回 null

Safe cast operation. Same as above, but instead of throwing an exception it will return null if cast operation fails.

提示:不要忘记将CultureInfo与 obj.ToString()一起使用Convert.ToString(obj)

Hint: Don't forget to use CultureInfo with obj.ToString() and Convert.ToString(obj)

12345.6D.ToString(CultureInfo.InvariantCulture);          // returns 12345.6
12345.6D.ToString(CultureInfo.GetCultureInfo("de-DE"));   // returns 12345,6
Convert.ToString(12345.6D, CultureInfo.InvariantCulture); // returns 12345.6
Convert.ToString(12345.6D, CultureInfo.GetCultureInfo("de-DE"));  // 12345,6
Convert.ToString(test);  // String.Empty, "test" is null and it's type
                         // doesn't implement explicit cast to string oper.
Convert.ToString(null);  // null
(string) null;           // null
(string) test;           // wont't compile, "test" is not a string and
                         // doesn't implement custom cast to string operator
(string) test;           // most likely NullReferenceException,
                         // "test" is not a string,
                         // implements custom cast operator but is null
(string) test;           // some value, "test" is not a string,
                         // implements custom cast to string operator
null as string;          // null

以下是自定义强制转换运算符的示例:

Here is an example of custom cast operator:

public class Test
{
    public static implicit operator string(Test v)
    {
        return "test";
    }
}

这篇关于.NET中的字符串强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆