Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for converting an object to string

Tags:

.net

vb.net

I've seen a number of ways of converting an Object to a String in .NET, typically for displaying the object's value to the user when the object type is not known.

These include:

Dim x as Object = 3
Dim y as Object = Nothing
Dim z as Object = DBNull.Value
Dim l_displayString As String

l_displayString = "" & x & "" & y & "" & z
l_displayString = If(x, "").ToString() & If(y, "").ToString() & If(z, "").ToString()
l_displayString = Convert.ToString(x) & Convert.ToString(y) & Convert.ToString(z)

Is there a method which is recommended by Microsoft, or do these all compile down to the same byte code?

EDIT:

Let me expand the question a little to include:

What are the differences between these methods? I can't see what's going on under the hood, so it'd be nice to know if there is any performance benefit of one over the others. In some cases, these calls may be made several thousand times (such as reading from a large table) and shaving off a couple seconds can make a big UX impact.

like image 976
JDB Avatar asked Jul 05 '12 15:07

JDB


People also ask

How do you transform an object into a string?

Convert Object to String in java using toString() method of Object class or String. valueOf(object) method. Since there are mainly two types of class in java, i.e. user-defined class and predefined class such as StringBuilder or StringBuffer of whose objects can be converted into the string.

Which method is used to convert an object into a string?

prototype. toString() The toString() method returns a string representing the object.

How will you convert a object to a string in Python?

Converting Object to String Everything is an object in Python. So all the built-in objects can be converted to strings using the str() and repr() methods.


2 Answers

The Convert.ToString(x) works gracefully even if x is null. In general, when dealing with stuff coming from database, I think Convert is the best approach. Another suggestion, when working with float/decimal numbers, keep an eye to the CultureInfo, ie don't trust the . as decimal sign, if you want to assume that use CultureInfo.InvariantCulture.

like image 162
Felice Pollano Avatar answered Sep 22 '22 07:09

Felice Pollano


They do different things. They compile to different MSIL code, but in most cases they will probably have the same result.

ToString is a method defined by Object, which is the inherent base type for all objects. By default it returns the object's type name, but it can (and often is) overridden by each type so that it returns a more meaningful string. For instance, in your example, x is an Int32 object, and Int32 overrides ToString so it returns "3" instead of the default "System.Int32".

I'm not positive, but I suspect when you do the concatenation "" & x, it is casting x to a String, in which case it is a shortcut to typing "" & CType(x, String) or "" & CStr(x). Each type can overload the casting operator, so it assumes that the type (in this case Int32) has overloaded the operator and can therefore be cast to a string. Indeed it has and can.

Convert.ToString does different things depending on which overload you call. If you pass it an Int32, it just calls the object's ToString() method. However, if you pass it an Object, for instance, it first checks to see if the object implements IConvertible or IFormattable. If it does, it uses one of those, otherwise it uses the ToString method. So, it tries, depending on the type of object you send it, to determine what it thinks it the most likely best way to get that type to a string.

As far as what is the preferred method, I would say x.ToString() is what you want to use most all of the time, unless you have some other concern (which all depends on what you are doing with the object).

like image 41
Steven Doggart Avatar answered Sep 23 '22 07:09

Steven Doggart