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
How do we achieve encapsulation in c#?
Is type nullable c#?
How do I automate my desktop application?
What is dll in vb.net?
What is an abstract class c#?
Explain the term inheritance in C#.
Overloaded constructor will call default constructor internally?
What are handlers in c#?
What is the purpose of ienumerable in c#?
Enlist all the components of an ado.net framework?
What is the advantage of constructor?
What is the difference between new and override in c#?
I have 3 overloaded constructors in my class. In order to avoid making instance of the class do I need to make all constructors to private?
What is difference between the "throw" and "throw ex" in .net?
Which is faster hashtable or dictionary?