本文介绍了Casting vs将对象转换为toString,当对象真的是字符串时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个问题,但我很好奇。当我保存一个字符串让我们说一个DataRow,它被转换为Object。当我想使用它,我不得不把它ToString。据我所知,有几种方法这样做,首先是

This isn't really an issue, however I am curious. When I save a string in lets say an DataRow, it is cast to Object. When I want to use it, I have to cast it ToString. As far as I know there are several ways of doing this, first is

string name = (string)DataRowObject["name"]; //valid since I know it's a string

,另一个是:

string name = DataRowObject["name"].ToString();

我感兴趣的是两者之间有什么区别?第一个更高效? (这只是一个猜测,在我的头ToString()方法是通过一些循环机制实现,只是投射它可以更快,但这只是一个直觉)。

I am interested in what is the difference between both? Is the first more efficient? (This is just a speculation, in my head ToString() method is implemented by some looping mechanism where just casting it "could" be faster, however this is just a "gut feeling" I have).

有更快/更优雅的方法吗?

Is there even a faster / more elegant way of doing this?

任何人都可以为我清除这个?

Can anyone clear this up for me?

推荐答案

href =http://www.codeguru.com/forum/showthread.php?t=443873 =nofollow noreferrer> http://www.codeguru.com/forum/showthread.php?t=443873

from http://www.codeguru.com/forum/showthread.php?t=443873

请参阅

这篇关于Casting vs将对象转换为toString,当对象真的是字符串时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:46