String and StringBuilder ... ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using VB.NET.

I have been reading up for an exam I will be taking in a few hours, and I'm
becoming confused... a String's contents never change? I read that every time
I change a String it's actually giving me a reference to a totally different
string; and that using a StringBuilder would be better if the String will be
changing a lot. I really don't understand all this... I also don't understand
really what the Stack and HashTable objects are good for, and I need to know
this as well. (I hope someone can answer these questions before I take the
exam.) Thanks.
 
Hello Alex,

Read explanations over here
http://www.yoda.arachsys.com/csharp/strings.html
http://www.yoda.arachsys.com/csharp/stringbuilder.html

AB> I'm using VB.NET.
AB>
AB> I have been reading up for an exam I will be taking in a few hours,
AB> and I'm becoming confused... a String's contents never change? I
AB> read that every time I change a String it's actually giving me a
AB> reference to a totally different string; and that using a
AB> StringBuilder would be better if the String will be changing a lot.
AB> I really don't understand all this... I also don't understand really
AB> what the Stack and HashTable objects are good for, and I need to
AB> know this as well. (I hope someone can answer these questions before
AB> I take the exam.) Thanks.
AB>
AB> ---
AB> Alex C. Barberi
AB> Chief Executive Officer
AB> VisionForce
AB> http://www.visionforceweb.com
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Alex said:
I'm using VB.NET.

I have been reading up for an exam I will be taking in a few hours, and I'm
becoming confused... a String's contents never change? I read that every time
I change a String it's actually giving me a reference to a totally different
string; and that using a StringBuilder would be better if the String will be

If you say (C# but you get the picture):

string s = "hi";

You've just created a new string object with all the methods that go
with it; same thing as:

string s;
s = new string("hi");

Now if you say:

s = s + " bob";
or
s = string.Format( "{0} bob", s );

s now contains the value "hi bob", but you've created a whole new string
object in the process (the original object referred to by s is now
waiting to be garbage collected).

On the other hand, if you say:

StringBuilder s = new StringBuilder("hi");
s.Append(" bob");

you've modified the string w/o creating a new object. If you're only
going to modify the string once or twice, you don't need the extra
overhead of the StringBuilder, but lots of string manipulation will be
more efficient with it.
changing a lot. I really don't understand all this... I also don't understand
really what the Stack and HashTable objects are good for, and I need to know
this as well.

A Stack is the quintessential data structure (actually both of these are
and can/should be studied in depth). It works simply thus:
LIFO: Last In First Out
You Push() objects in and Pop() them out. Pop() gives you the last
object you pushed in and removes it from the collection. Peek() gives
you (a reference to) the last item pushed in w/o removing it.

The HashTable is a collection that allows you to throw objects in and
then retrieve them again by referencing the object's key. The key is
simply another object, so the simplest example is a string:

HashTable ht = new HashTable();
MyObject first = new MyObject();
MyObject second = new MyObject();
ht.Add( "first", first );
ht.Add( "second", second );
// later on...
MyObject myFirst = ht["first"];
 
Another important point is that when you concatenate string literals
the C# Compiler would concatenate them in to one string instead of
creating multiple strings.

Example

string test = "Test" + "test123";

the compiled IL would be which is nothing but "Testtest123".

..locals init (string V_0)
IL_0000: ldstr "Testtest123"
IL_0005: stloc.0
 
Back
Top