C# .NET - Adding two strings - Asked By Lokesh M on 25-May-13 12:48 AM

Hi,

string str1 = "abcd";
string str2 = "efgh";
string str3 = str1 + str2;

while executing "str1+str2", which method will call in string class.

is it operator overloading or concat method ?
or How it internally works ?

Regards
Lokesh.
Bill B replied to Lokesh M on 14-Jun-13 04:48 AM
strings are immutable in .net

internally a string is represented by a char array which cant be resized?

http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=118800

http://channel9.msdn.com/forums/TechOff/58729-Why-are-string-types-immutable-in-C/

http://www.codeproject.com/Articles/6852/Strings-in-NET
Lokesh M replied to Lokesh M on 11-Jul-13 06:43 AM
Hi,

SECTION1:
writing like this: 
string str3 = str1 + str2;

On viewing the above line in ILDASM.exe, and it s re-written in intermediate language as below:
System.String::Concat(string, string)

which means that Concat method of string class will be get called:
public static string Concat(string str0, string str1)

SECTION2:
similary, writing like this
if(str1 == str2)

On viewing the above line in  ILDASM.exe, and it s re-written in intermediate language as below:
bool [mscorlib]System.String::op_Equality(string,string)
( http://msdn.microsoft.com/en-us/library/system.string.op_equality.aspx )
which indicates that, below method of string class will get called:
public static bool operator ==(string a, string b) { }
Regards Lokesh.