ismailp said:
First of all, in unmanaged code, if you assign a constant string to a
char*:
const char* szText1 = "Hello";
const char* szText2 = "Hello";
compiler generates one "Hello" and both szText1 and szText2 point the
same location.
Actually it is not guaranteed that they point to the same location on this. A compiler is
free to optimise them to one string literal, however this is not guaranteed, and we should
not rely on this. If we want to make sure that they point to the same place, we must make
a pointer comparison.
Very same rule applies to managed applications as well.
What happens when you "type" inside the textbox deals with Windows.
Actually when we type in a .NET TextBox, our characters get added to the System::String
pointed by the Text property. My question was if a new String object gets created
implicitly, or in effect, we are on the string literal in this case.
After all, it is a Windows "EDIT" control. Read GetWindowText on MSDN
to learn more about how windows manage their window texts.
I assume this is some Win32 API function. However I do not know Win32 API.
So,
String* pString1 = "Hello";
String* pString2 = "Hello";
will point the same location - no new string is created for pString2.
However, a new String object is constructed, and these two pointers
will be pointing to that object.
If implicit String creation takes place, I do not think both of these will be pointing to
the same object, the same as the case
String *pString1= __gc new String("Hello");
String *pString2= __gc new String("Hello");
or the case of standard C++:
string s1= "Hello";
string s2= "Hello";
The above two string objects are not the same one.
(Write a sample, and open
disassembler, see "mov ecx,.." sections. ECX is "this" pointer.)
..NET has its own assembly language and .NET assembler/disassembler. And I do not know IL
assembly so far.