473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

obj.ToString(); vs obj + "";

Hi.

obj.ToString();

or

obj + "";

Which is more efficient and why?
Sep 3 '08 #1
7 1658
Which is more efficient and why?

By a *very* small margin, the first. The second compiles to
string.Concat(obj) [note that the + "" is optimised away]. This is an
additional method call (which is possibly inlined), but which does an
additional null check (returning "" if obj is null) before calling the
same obj.ToString().

The difference will be stupidly small, and is unlikely to *ever* show
on any performance trace - so go with whichever is simplest to read.
For me, the first wins this. Note that they behave differently for obj
= null, though.

Marc
Sep 3 '08 #2
Ray Proffitt wrote:
Hi.

obj.ToString();

or

obj + "";

Which is more efficient and why?
The first is slightly more efficient.

Also, it's better from a maintainability perspective. The first code
does exactly what it says, nothing more and nothing less. The second
code describes a nonsense-operation that only has the purpose of causing
an implicit conversion.
--
Göran Andersson
_____
http://www.guffa.com
Sep 3 '08 #3
Ray,

No-one has mentioned the case of obj==null. Makes the two statements quite
different.

Hilton
"Ray Proffitt" <ra****@gmail.comwrote in message
news:cc**********************************@a3g2000p rm.googlegroups.com...
Hi.

obj.ToString();

or

obj + "";

Which is more efficient and why?

Sep 3 '08 #4
No-one has mentioned the case of obj==null.

Except for: "but which does an additional null check (returning "" if
obj is null) "
and "Note that they behave differently for obj = null, though."

Marc
Sep 3 '08 #5
Thanks folks... It's ToString() from now on; always prefered it
anyway.

Sep 3 '08 #6
Ray Proffitt wrote:
obj.ToString();

or

obj + "";

Which is more efficient and why?
The first, but it does not matter.

What is important is what code clearly expresses
the intent of the code.

And in that aspect the first wins huge.

Arne
Sep 4 '08 #7
Convert.ToString(obj). Clear. Safe.

Otherwise, if you good in checking of nulls use your first one. It's much
obvious.

Regards, Alex Meleta
mailto:am*****@gmail.com; blog:devkids.blogspot.com
Ray Proffitt wrote:
>obj.ToString();

or

obj + "";

Which is more efficient and why?
The first, but it does not matter.

What is important is what code clearly expresses
the intent of the code.
And in that aspect the first wins huge.

Arne

Sep 4 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olaf Baeyens | last post by:
A question: (C#, C++) If I use iValue.ToString("000000000000",CultureInfo::CurrentUICulture)); The I get a string with a fixed lengt if format: "000000542663" But since I want the leading "0"...
6
by: trint | last post by:
Here is the vb.net version of what I need to do in c#: myString = drSQL.Item("columnName").ToString() Thanks, Trint
1
by: Steve Norman | last post by:
Hi All, I am trying to display an int as it' sHex representation, and looking on MSDN it appears this can be done with .ToString("X"). This does work, but the example on MSDN...
0
by: Norman Yuan | last post by:
I posted this issue a while ago. After moveing the ASP.NET 1.1 app to a brand new server, the same problem still there. System: Winows2003 server std. .NET 1.1 App: Invoicing system Problem:...
1
by: Jason Chan | last post by:
DateTime mydate = new DateTime(2006,1,1,0,0,0); string testStr = mydate.ToString("hh:mm:ss"); //return 12:00:00 mydate = new DateTime(2006,1,1,1,0,0) testStr = mydate.ToString("hh:mm:ss");...
3
by: Geoffrey | last post by:
Hello, with byte.toString("X"), if the decimal value of the byte is <16, then that return opnly one character. Is it possible (with a short things like another formatprovider than "X") to...
4
by: Larry Smith | last post by:
Once you call "ToString()" for an object, how can you then convert the string back to the original object (assuming you know its type of course)? Also, why does "ToString()" produce one string and...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
1
by: Marc Gravell | last post by:
Just been debugging a daylight savings glitch... something that interested me: (in a BST locale at 5:14 local time, 4:14 GMT) DateTime.Now.ToString("R") >"Mon, 26 Mar 2007 05:14:34 GMT" ...
5
by: Bob | last post by:
Hello Folks, I am bring backa data reader dr from the database/ So I have a number of fields such as dr To put this in a table I use "<td>" + dr.ToString() + "</td>" But I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.