Is string reference type / value type?

Answer Posted / pradeep chouhan

Hi Friends,

string is a reference type.

kindly go through below code.

string str1 = "1";
Response.Write(str1+"<br/>"); //output: 1
string str2 = str1;
str2="2";
Response.Write(str1+"<br/>"); //output: 1 (instead of 2)

Bcos of this behavior of string most of the people think
string is a value type. but the actual mystery lies on the
3rd line of the above code ie string str2 = str1; Always
remember '=' assignment operator is overloaded by default
for string. so when str2 = str1 this means a copy of str1 is
assigned to str2 instead of its reference.

I think now the idea is clear.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

if you do have a stack overflow profile.what is your ranking?

1493


What is oledb in c#?

513


What does clr stand for?

490


Why generics are used?

505


Can we have only “try” block without “catch” block in c#?

483






What is c# console application?

492


Why do we use reflection in c#?

504


What is the difference between a field and a property in c#?

506


What is difference between dictionary and hashtable in c#?

444


In a C# class we have a SortedList member m_addinProjects we want to provide an iterator to allow the consumer of this class access to the items in the collection. Please provide an iterator method for the AnalyzeAddinsDLL class below and an example of how it would be used. namespace AnalyzeAddinsDLL { public class AllAddInProjects { private SortedList m_addinProjects; public AllAddInProjects() { m_addinProjects = new SortedList(); } } }

1824


What is difference between web and window application?

411


What is difference between array and list in c#?

478


What is scaffolding in c#?

490


Explain the difference between a namespace and assembly name in .net?

509


What is the difference between early binding and late binding in c#?

513