转换和分析之间的区别是什么? [英] What is the difference between Convert and Parse?

查看:185
本文介绍了转换和分析之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以写下面将对象转换为整数。

  Convert.ToInt32(myObject的);
 

不过,我也可以这样写

  Int.Parse(myObject.ToString());
 

  • 有什么区别?
  • 在哪一个我应该使用?

在此先感谢。

解决方案
  
      
  • 有什么区别?
  •   

Int32.parse(myObject.ToString()); 需要绕道而行,串,通常会工作,但它是不必要的,它可能会失败或给予不同的结果。

  
      
  • 在哪一个我应该使用?
  •   

在一般情况下, Convert.ToInt32(myObject的);

不过,这取决于你想要转换什么类型的数据。

如果 myObject的='1'; ,你要不要 1 49

如果 myObject的= FALSE; ,做你想做 0 或异常

I could write the following to convert an object to an integer.

Convert.ToInt32(myObject);

But I could also write

Int.Parse(myObject.ToString());

  • Is there any difference?
  • Which one should I be using?

Thanks in advance.

解决方案

  • Is there any difference?

Yes, Int32.parse(myObject.ToString()); takes a detour to string, that will usually work but it is unnecessary and it might fail or give a different result.

  • Which one should I be using?

In general, Convert.ToInt32(myObject);

But it depends on what type of data you want to convert.

If myObject = '1'; , do you want 1 or 49 ?

If myObject = false; , do you want 0 or an exception ?

etc

这篇关于转换和分析之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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