问题描述
转换类型时,我发现自己使用了VB函数和BCL Convert.To *方法。
例如)
-
Cstr()
与Convert.ToString()
-
CInt()
与Convert.ToInt32()
-
CDbl()
与Convert.ToInt64()
- etc ...
是否有细微的差异值得注意?
这已经在之前的原则,但是有不同之处:基本上VB助手会为你做额外的工作,以便通过泛型将引发异常的地方进行解析,并且通常但不是普遍的VB助手更快(尽管我不知道是否它是如此),因为它们真的只是IL糖。季节味道。
编辑:比我能做得更好。
Edit Redux:Joel Coehoorn也建议,显然有一些在他的袖子里找个地方。
Joel写道:
While converting types, I have found myself using both VB functions and BCL Convert.To* methods.
E.g.)
Cstr()
vs.Convert.ToString()
CInt()
vs.Convert.ToInt32()
CDbl()
vs.Convert.ToInt64()
- etc...
Are there any subtle differences that should be noted?
This has been covered before in principle, but yes there are differences: basically the VB helpers will do additional work for you to get the parse through where the generics will throw an exception, and in general but not universally the VB helpers are faster (though I don't know if it's significantly so) because they're just IL sugar really. Season to taste.
Edit: This guy covers it better than I can.
Edit Redux: Joel Coehoorn also recommends the precursor to the above article, and apparently has some benchmarking up his sleeve somewhere.
Joel wrote:
这篇关于*微妙* VB函数和Convert.To *函数之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!