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
Explain metadata in c#.
Explain the difference between // comments, /* */ comments and /// comments?
How much time will it take to learn unity?
Why do I get a "cs5001: does not have an entry point defined" error when compiling?
Define Abstract Class in C#
what is inheritance and an example in vb.net and c# of when you might use it?
Why generics are used in c#?
What is parallel programming in c#?
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
What is the purpose of private constructor in c#?
What is the purpose of namespace in c#?
What is session c#?
Is string nullable in c#?
Which .gang of four. Design pattern is shown below?
Why do we need events in c#?