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
Why do we use ienumerable in c#?
What is public or shared assemblies ?
Why do we use class in c#?
What is the do while loop code?
What is a console operator?
Is for loop faster than foreach?
Explain the difference between user control and custom control. Also, explain their use.
Is c# a technology?
How can encapsulation be achieved?
How do I create a multi language, multi file assembly?
How do I know if executenonquery is successful c#?
What is uint c#?
What is the difference between package and interface?
What is the use of getcommandlineargs() method in c#.net?
Which is executed if an exception has not occurred?