*微妙* VB函数和Convert.To *函数之间的差异? [英] *Subtle* Differences between VB functions and Convert.To* functions?
问题描述
转换类型时,我发现自己使用了VB函数和BCL Convert.To *方法。
例如)
-
Cstr()
与Convert.ToString()
-
CInt()
与Convert.ToInt32()
-
CDbl()
与Convert.ToInt64()
- etc ...
是否有细微的差异值得注意?
这已经在 之前的原则,但是有不同之处:基本上VB助手会为你做额外的工作,以便通过泛型将引发异常的地方进行解析,并且通常但不是普遍的VB助手更快(尽管我不知道是否它是如此),因为它们真的只是IL糖。季节味道。
编辑:这家伙比我能做得更好。
Edit Redux:Joel Coehoorn也建议上述文章的前体,显然有一些在他的袖子里找个地方。
Joel写道:
CInt()是
操作符,而Convert.ToInt32()是
函数。 (int)x之间的CInt居住在
之间;和
Convert.ToInt32(x);。
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:
The summary is the CInt() is an operator, while Convert.ToInt32() is a function. CInt lives somewhere in between (int)x; and Convert.ToInt32(x);.
这篇关于*微妙* VB函数和Convert.To *函数之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!