加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:27:53 所属栏目:大数据 来源:网络整理
导读:我想知道哪个更有效率,使用CStr()或object.toString(). 我问这个的原因是因为我尽管所有的CStr()完成是调用它所处理的对象上的.ToString()方法. 但是当最近使用没有任何类型约束的泛型方法时,我不得不使用object.ToString()而不是CStr(object),以下仅仅是一
我想知道哪个更有效率,使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读