string string1 = "This is a string";
string string2;
string2 = Convert.ToString(string1);
I could have simply said string2 = string1; and it have been functionally the same. Is that optimized out during the compile stage?
string1 and string2 are references to objects in memory. If you do string2 = string1;, they both now point to the same object in memory. Normally, this would mean that if you modify the object, say if you add an item to an array list, you will see the newly added object whether accessing the object from one or the other reference. This is not so different from a pointer pointing to a memory location in C.
However, strings are, by design, immutable. There is no way you can modify the memory that the object is located at. string2 = string1; is therefore perfectly safe and sensible. If you later do string2 = "Another string"; a new string object is created, and the string2 reference ("pointer") is now made to point to that object instead, and string1 is left intact.
string2 = Convert.ToString(string1); may or may not be optimized depending on the implementation. Convert.ToString may choose to return the same object if the object is of type string instead of creating a new one. Bottom line is, that that shouldn't matter for strings and other immutable objects, as far as correct execution is concerned.
Should? Should. There is a common pitfall, at least in java, and quite possibly in c#, which is that the == operator compares the references and not the contents of the objects. Something like:
string string1 = "a string";
string string2 = "a string";
if (string1 == string2){
Console.Write("true")
}else{
Console.Write("false")
}
would then report false, since the strings are stored in separate objects. To confuse things further, the compiler might be smart enough to point string1 and string2 to the same reference, as their contents are identical and they appear in the same scope. The comparison above might then be true, while the same comparison made from user input would be false.
The correct way to compare strings is with Equals: if (string1.Equals(string2))
I also see that I can do a lot of crazy things that I find myself saying "well this seems logical but there's no way it would work" but it does work.
Example:
This is a line from a program I'm working on (no I'm not asking for homework help on this). The line is just there to give me some status as to what my variables are doing as I'm working on part of it.
wordGuess is an array of chars and I'm outputting it is a string. I had tried to use the ToString method but apparently you can't do that for whatever reason with an array of chars. So I did some research and found out about using "new string" but the example was something like:
string s = new string(c);
In my case I didn't need to assign it to anything, I just wanted to monitor its status my program did its thing, so I thought, what if I just omit the assignment?
So here I create a new string out of thin air with no variable for it to go into, and it works.
Console.WriteLine("{0} {1} {2}", wordList[rndNum], index, new string (wordGuess));
Is this some kind of on the fly thing that disappears or does it actually take a small snip of memory that it doesn't give back. If I ran it through an infinite loop, would the program eventually crash because of eating up too much resources?
Unlike a language like c++, c# has a garbage collector, whose job it is to look for unreferenced objects and recycle their resources. Unless garbage collector leaks resources, that line should not create any problem.
To aid your mental model, new string (wordGuess) is an expression. Expressions can generically be used both in a function call or in an assignment, or even on its own.
new string (wordGuess) <- Example of an expression
string s = <some expression>;
Console.WriteLine(<some expression>);
<some expression>;
So:
string s = new string (wordGuess);
Console.WriteLine(new string (wordGuess));
new string (wordGuess);
The last line is of course utterly useless for strings. But if you were creating an object from a class you wrote, that does something useful in its constructor, but that you don't need a reference to, then that code is a valid, though quirky way of creating an object of that type.
Going on, the reason that you cannot do a ToString on a char array, is that a char array, much like an int or a float, is not an object. Thus it has no methods you can execute. You should still be abloe to convert it into a string using Convert.ToString(chararray) or new string(chararray).