Csharp/C#教程:字符串强制转换分享


字符串强制转换

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

Convert.ToString(obj)

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

obj.ToString()

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

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

(string)obj

这是一个强制转换操作,而不是函数调用。 如果您确定该对象的类型为字符串,或者它具有可以将其转换为字符串的隐式或显式运算符,请使用它。 如果对象为null AND of type String or of type which implements custom cast to string operator. See examples.则返回null null AND of type String or of type which implements custom cast to string operator. See examples. null AND of type String or of type which implements custom cast to string operator. See examples.

obj as string

安全铸造操作。 与上面相同,但是如果转换操作失败,它将返回null ,而不是抛出exception。


提示 :不要忘记将CultureInfo与obj.ToString()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 

以下是自定义强制转换操作符的示例:

 公共课测试  {      public static隐式运算符字符串(Test v)      {         返回“测试”;      }  } 

如果对象为null,Convert.ToString()将返回一个空字符串.ToString和(String)将引发exception。 如果值为null,Convert.ToString将在内部调用.ToString(),它将返回一个空String。

object.ToString()是检索对象的字符串表示的最基本方法,可以由对象专门实现。

Convert.ToString()扩展了它并为原始类型(char,byte,int,double等)提供了一些特定的重载,允许一些更特定于类型的function(例如,基本转换)

(string)是一个转换运算符,只有在类型是字符串或者具有可以将其转换为字符串的隐式或显式运算符时才会起作用。 否则,您将获得InvalidCastException

不要忘记as string

ToString()是一个对象的方法,它总是在非空引用上工作,所以你会得到一些东西,但是这个东西是否是你想要的东西,是一个不同的故事。

在大多数情况下,Convert.ToString()将产生相同的结果,但不像Object.ToString()那样灵活,因为您无法传递自定义格式规则。

(字符串)将您的对象转换为字符串,如果它不是字符串,那么您将获得InvalidCastException()。

认为。

ToString是一个虚方法,每种类型都可以实现它想要的。 System.Object也提供默认实现,以便它始终成功。 Convert.ToString也适用于空值,并允许您使用注释中指出的IFormat provier。

转换为字符串需要对象来实现转换操作符。 同样,类型可以实现它,但他们喜欢,但大多数类型没有,所以你可能会在这里得到一个例外。

使用.ToString作为最佳选择。

.ToString()是一个实例方法,它询问对象的字符串表示。 当对象为null时,这将引发exception。

(string)是对字符串类型的强制转换,除了简单的数据类型之外,在大多数情况下这不是一个好主意,因为当它为null或无效的强制转换时它可以中断(抛出exception)

Convert.ToString()比简单的强制转换做了更多的检查,为强制转换提供了更强大的替代方案。 当对象为null时,它将返回空字符串。

不要挑剔但nullString对象的有效值。 因此(string) null不会抛出任何exception。 试试吧:

上述就是C#学习教程:字符串强制转换分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 using System; namespace Test { class Program { public static void Main(string[] args) { string s = (string) null; Console.WriteLine(s); } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1042812.html

(0)
上一篇 2022年1月30日
下一篇 2022年1月30日

精彩推荐