Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting vs Converting an object toString, when object really is a string

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 

and another one is:

string name = DataRowObject["name"].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?

like image 368
David Božjak Avatar asked Jul 23 '09 09:07

David Božjak


People also ask

What is the difference between convert to string and ToString?

Here both the methods are used to convert the string but the basic difference between them is: "Convert" function handles NULLS, while "i. ToString()" does not it will throw a NULL reference exception error. So as good coding practice using "convert" is always safe.

Should Convert object into a string?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else. Here, we are going to see two examples of converting Object into String.

What happens if you call ToString on a string?

ToString is a method defined in the Object class, which is then inherited in every class in the entire framework. The ToString method in the String class has been overwritten with an implementation that simply returns itself. So there is no overhead in calling ToString() on a String-object.


2 Answers

The two are intended for different purposes. The ToString method of any object is supposed to return a string representation of that object. Casting is quite different, and the 'as' key word performs a conditional cast, as has been said. The 'as' key word basically says "get me a reference of this type to that object if that object is this type" while ToString says "get me a string representation of that object". The result may be the same in some cases but the two should never be considered interchangeable because, as I said, they exist for different purposes. If your intention is to cast then you should always use a cast, NOT ToString.

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

see also http://bytes.com/groups/net-c/225365-tostring-string-cast

like image 72
Adriaan Stander Avatar answered Oct 21 '22 20:10

Adriaan Stander


If you know it is a String then by all means cast it to a String. Casting your object is going to be faster than calling a virtual method.

Edit: Here are the results of some benchmarking:

============ Casting vs. virtual method ============ cast 29.884 1.00 tos  33.734 1.13 

I used Jon Skeet's BenchmarkHelper like this:

using System; using BenchmarkHelper;  class Program {     static void Main()     {         Object input = "Foo";         String output = "Foo";          var results             = TestSuite.Create("Casting vs. virtual method", input, output)             .Add(cast)             .Add(tos)             .RunTests()             .ScaleByBest(ScalingMode.VaryDuration);          results.Display(ResultColumns.NameAndDuration | ResultColumns.Score,                 results.FindBest());     }      static String cast(Object o)     {         return (String)o;     }      static String tos(Object o)     {         return o.ToString();     } } 

So it appears that casting is in fact slightly faster than calling ToString().

like image 40
Andrew Hare Avatar answered Oct 21 '22 21:10

Andrew Hare