当使用CAST或CONVERT [英] When to use a Cast or Convert

查看:126
本文介绍了当使用CAST或CONVERT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,想知道其中的差别是投之间说什么一个int相比,使用Convert.toInt32()。是否有某种性能增益使用吗?

I am curious to know what the difference is between a cast to say an int compared to using Convert.toInt32(). Is there some sort of performance gain with using one?

也应该每个被使用的情况。目前,我更倾向于使用转换,但我没有理由去两种方式。在我心目中,我看到他们都完成同样的目标。

Also which situations should each be used for. Currently I'm more inclined to use Convert but I don't have a reason to go either way. In my mind I see them both accomplishing the same goal.

推荐答案

请参见之间的差异CAST和CONVERT 在另一个论坛

该Convert.ToInt32(字符串的IFormatProvider)下调用Int32.Parse。因此,唯一的区别是,如果一个空字符串
  传递给转换则返回0,而Int32.Parse抛出
  ArgumentNullException。这的确是一个选择的问题无论你
  使用。

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. It is really a matter of choice whichever you use.

就个人而言,我既不使用,并趋向于使用功能的TryParse,如<一个href=\"http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx\">System.Int32.TryParse()

Personally, I use neither, and tend to use the TryParse functions, as in System.Int32.TryParse()

这篇关于当使用CAST或CONVERT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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