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
What are the different types of assembly?
What is appdomain in c#?
Can hashset contain duplicates c#?
What are the benefits of using the aggregate method in linq?
What are primitive data types in c#?
What is an ienumerable in c#?
What is window application in c#?
Is it possible to nest cfml conditional tags?
Can namespace contain the private class?
Is int a class in c#?
Can we have static indexer in c#?
Are multiple data types stored in System.Array?
What is the difference between static and private constructor in c#?
What are scriptable objects?
What is executenonquery c#?