nwcomer


NameTable nameTable = new NameTable();

string str1 = "sunny";
StringBuilder strBuilder = new StringBuilder();
string str2 =
strBuilder.Append(
"sun").Append("ny").ToString();
Console.WriteLine("{0} : {1}",
str1, str2);
Console.WriteLine("{0} : {1}",
str1 == str2,
(
Object)str1 == (Object)str2); // why this is False
string str3 = nameTable.Add(str1);
string str4 = nameTable.Add(str2);
Console.WriteLine("{0} : {1}",
str3, str4);
Console.WriteLine("{0} : {1}",
str3 == str4,
(
Object)str3 == (Object)str4); // why this is now true


and if i modify the above code like this


NameTable nameTable = new NameTable();

string str1 = "sunny";

string str2 = "sunny";

Console.WriteLine("{0} : {1}",
str1, str2);
Console.WriteLine("{0} : {1}",
str1 == str2,
(
Object)str1 == (Object)str2); // why this is True now
string str3 = nameTable.Add(str1);
string str4 = nameTable.Add(str2);
Console.WriteLine("{0} : {1}",
str3, str4);
Console.WriteLine("{0} : {1}",
str3 == str4,
(
Object)str3 == (Object)str4); // This is also true




Re: Visual C# Language (Object) casting Confusion


Re: Visual C# Language (Object) casting Confusion

nwcomer

Thanks a lot, but still some confusion.



Re: Visual C# Language (Object) casting Confusion

IsshouFuuraibou

to point out another aspect of this:

string == string is comparing values of the string
object == object is comparing the reference (pointer) not the actual values.

When using string builder, even though you end up with the same string, you created it during runtime not compile time, so it would have a different reference.

The NameTable manages entries to avoid duplicates and to save space.
NameTable Class states
When an element or attribute name occurs multiple times in an XML document, it is stored only once in the NameTable.


And the retuen of the nameTable.Add( string ) is "The atomized string or the existing string if it already exists in the NameTable."

Since "sunny" (str1) and "sunny" (str2) are the same, when using the add method for str2, the add function stores only one "sunny" string and returns the same string in both add statements.




Re: Visual C# Language (Object) casting Confusion

OmegaMan

nwcomer wrote:
Thanks a lot, but still some confusion.


There are two components of a string, one is its reference which sits on the stack (in your example) and points to the actual data which sits on the heap. What happens is that you are comparing references and not the actual values and that is leading to the confusion.





Re: Visual C# Language (Object) casting Confusion

nwcomer

Thanks.

According to IsshouFuuraibou

string == string is comparing values of the string
object == object is comparing the reference (pointer) not the actual values.

Say If I have

string str1 = "sunny";

string str2 = "sunny";

Console.WriteLine("{0} : {1}",

str1, str2);

Console.WriteLine("{0} : {1}",

str1 == str2,

(Object)str1 == (Object)str2); // why this is true, since str1 and str2 are different string objects and hold different references as though both the string values are same.






Re: Visual C# Language (Object) casting Confusion

IsshouFuuraibou

In Mathew's post:
http://www.sliver.com/dotnet/emails/default.aspx id=6
explains that.

During compilation (JIT compilation) the CLR managed a hashtable and only creates one "sunny" since it is inline code evaluated at compile time. So both the first and second "sunny" are the same string in memory.

The CLR maintains a hashtable of strings on a per-process basis; they're shared across all app domains in that process. When a method is JITted, the runtime takes all the strings in that method and adds them to the hashtable, if they don't already exist. This lessens the number of string refernces necessary in the process, sometimes significantly.





Re: Visual C# Language (Object) casting Confusion

OmegaMan

IsshouFuuraibou wrote:
In Mathew's post:
http://www.sliver.com/dotnet/emails/default.aspx id=6
explains that.

During compilation (JIT compilation) the CLR managed a hashtable and only creates one "sunny" since it is inline code evaluated at compile time. So both the first and second "sunny" are the same string in memory.

The CLR maintains a hashtable of strings on a per-process basis; they're shared across all app domains in that process. When a method is JITted, the runtime takes all the strings in that method and adds them to the hashtable, if they don't already exist. This lessens the number of string refernces necessary in the process, sometimes significantly.


As referenced back to my post, the actual strings are contained on the heap and not the stack. The CLR is optimized to not duplicate strings. The references are separate on the stack and by being a reference have different addresses.

Think of a reference as a letter with an address on it. If you go to that address you will find a house. There may be may letters with that house address, but only one house.





Re: Visual C# Language (Object) casting Confusion

IsshouFuuraibou

To sum it all up:

String comparison compares values, while object comparison compares references.

String store in two parts: a reference that resides on the stack, and a value that resides in the heap. Object comparison looks to see if the two references are pointing to the same object in the heap.

The CLR, and for that matter the NameTable, is optimized to not generate duplicate strings. The CLR manages a hashtable per process that is populated with strings from methods. This hashtable would be located in the heap. When a method is JITted, strings used are added to this hash if they do not exist yet. However, if they exist they are not added a second time but actually use the currently existing string value from the hashtable. NameTable does the same automatically during initialization and when you manually call the Add method on a NameTable.

so when you do:
string str1 = "sunny";
string str2 = "sunny";

One value gets created, two strings get created that both reference this one value even though they are different string object.

The same thing occurs when you do
string str3 = nameTable.Add(str1);
string str4 = nameTable.Add(str2);

Because string is immutable, if you were to then go do
str3 = "cloudy";
it would make a new string value in the hashtable and then update the reference inside the str3 object leaving str4 the way it was.

Does this clarify your problem





Re: Visual C# Language (Object) casting Confusion

nwcomer

Thanks Guys- Really appreciate that.

Regards,