vb.net – 哪个更高效Cstr(value)或value.ToString()

我想知道哪个更有效率,使用Cstr()或object.toString().
我问这个的原因是因为我尽管所有的Cstr()完成是调用它所处理的对象上的.ToString()方法.

但是当最近使用没有任何类型约束的泛型方法时,我不得不使用object.ToString()而不是CStr(object),以下仅仅是一个例子来说明问题.

Public Function IDFromObject(Of ID_TYPE)(ByVal value As ID_TYPE) As String
    Return value.ToString
End Function

按预期编译,但以下没有使用Cstr().它给出了类型为ID_TYPE的编译错误值不能转换为字符串.但它显然可以使用.ToString()

Public Function IDFromObject(Of ID_TYPE)(ByVal value As ID_TYPE) As String
    Return CStr(value)
End Function
here(不能说更好):

CStr is a keyword,whereas ToString is
a function (method). CStr is compiled
inline and it creates code depending
on the type of the passed object. It’s
mainly there for people being used to
it from prevIoUs VB versions. I
haven’t used CStr in .Net anymore
(because it’s not obvIoUs what it does
in which situations and it’s also not
very well documented).

The difference depends on which ToString function you use. Every type can have it’s own implementation.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...