ToString()

A

Alvin Bruney

I was taught at aspnetpro seminar (which the company paid good bucks for)
that tostring doesn't actually create a string object or that it doesn't do
a conversion of the specified value to its equivalent string representation,
rather it returns a reference pointing to an internal representation of the
object (already implemented) making the tostring really fast and efficient
with very little over head.

Is this correct? Does any body know? Show me. I want to see for myself cuz i
don't like repeating stuff. His teaching contradicts the docs by the way.

This teaching is very difficult for me to accept. But otherwise the teacher
is extremely knowledgeable.
 
J

Jon Skeet [C# MVP]

I was taught at aspnetpro seminar (which the company paid good bucks for)
that tostring doesn't actually create a string object or that it doesn't do
a conversion of the specified value to its equivalent string representation,
rather it returns a reference pointing to an internal representation of the
object (already implemented) making the tostring really fast and efficient
with very little over head.

Is this correct? Does any body know? Show me. I want to see for myself cuz i
don't like repeating stuff. His teaching contradicts the docs by the way.

This teaching is very difficult for me to accept. But otherwise the teacher
is extremely knowledgeable.

ToString() of which type? object.ToString() just returns the name of
the type, and anything else can do whatever it likes. String.ToString()
clearly doesn't need to create a new string, whereas
StringBuilder.ToString() obviously does.

Which type was your teacher talking about?
 
P

Peter Rilling

I am not sure what your instructor meant but the implementation of ToString
is dependent on how the developer decided to write it. Since any object can
override this method, they could make it as simple or complex as they want.

My personal beliefs about seminars is that they only put money in the
pockets of those giving the seminars. A good book is really all you need to
learn technologies.
 
D

David Browne

Alvin Bruney said:
I was taught at aspnetpro seminar (which the company paid good bucks for)
that tostring doesn't actually create a string object or that it doesn't do
a conversion of the specified value to its equivalent string representation,
rather it returns a reference pointing to an internal representation of the
object (already implemented) making the tostring really fast and efficient
with very little over head.

Is this correct? Does any body know? Show me. I want to see for myself cuz i
don't like repeating stuff. His teaching contradicts the docs by the way.

In general, no, ToString usually creates a new string and populates it with
some data about the object.

There are special cases, though, where this happens. For instance,
StringBuilder.ToString, and, of course String.ToString behave basically as
he described.

David
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
ToString() of which type? object.ToString() just returns the name of
the type, and anything else can do whatever it likes. String.ToString()
clearly doesn't need to create a new string, whereas
StringBuilder.ToString() obviously does.

Slight correction having looked at David Browne's post:

StringBuilder.ToString sometimes does create a new string, and
sometimes doesn't. When it doesn't, any subsequent mutating operation
(e.g. Append) on the StringBuilder creates a new string instead.
 
A

Alvin Bruney

I believe he was talking about Tostring on stringbuilder because i was
concerned about performance after using a stringbuilder to get away from
temporary objects and then calling tostring which created a temporary
object.

i am having an unnatural compulsive obsession with performance. need a 12
step program.
 
A

Alvin Bruney

My personal beliefs about seminars is that they only put money in the
pockets of those giving the seminars. A good book is really all you need to
learn technologies.

some days i agree with that statement. this seminar, or workshop was really
good. and besides, i needed to get away from the stifling pressure at work.
jeez i swear somedays they want to kill me. everybody's got these days
right??
 
J

Jon Skeet [C# MVP]

I believe he was talking about Tostring on stringbuilder because i was
concerned about performance after using a stringbuilder to get away from
temporary objects and then calling tostring which created a temporary
object.

Ah, right. Yes, StringBuilder is nicely done so that if you don't use
it again after calling ToString, it's that bit more efficient.
 
D

Daniel Billingsley

Hey, watch your tongue there buddy. My company sent me to VSLive! Orlando
last Fall and I got to stay in a resort hotel in Orlando for most of a week.
Don't start spreading rumors the owners might hear that they're a waste of
time and money! LOL! (that one wasn't, btw)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top