Is string a value type or a reference type?

Answer Posted / benjamin janecke

String is a reference type but it behaves somewhat like a
value type. So it's a bit tricky and many developers don't
know it.

Great care should be taken when doing excessive string
manipulations with the String class. As already posted,
each String is read only and can never be changed. Thus,
every manipulation creates a new instance (and causes the
garbage collector to remove old ones) which can have a
dramatic performance impact on an application. Therefore,
always use the StringBuilder class in these scenarios,
which holds only one representation of a string in memory
and performs any changes directly to that memory block.

Btw. for those who don't belive String is a reference type,
just refer to MSDN or run ILDASM und load the mscorlib.dll
module from the .NET Framework. Then go to System.String
and u'll notice it's directly inherited from System.Object -
contrary to, for instance, the System.Byte class which is
inhertied from System.ValueType, meaning Byte is a value
type, String is not.

Is This Answer Correct ?    36 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between web api and web services?

725


What are Master Pages in ASP.NET?

770


Explain ViewState?

770


Define managed code and managed data in .net?

697


if i want to give an alert message like "try after sometime" to a web page which is being seen by other person.if a web page is not seen by anyone then it should display otherwise it show a display a message stating that other person is viewing so try after some time........how can i implement this.

2015


What is the viewstate in asp.net?

762


Explain asp.net page life cycle?

830


What is a server farm in iis?

729


What is data reader in asp.net?

744


Explain managed code an un-managed code.

770


What is globalization and localization in asp net?

693


How asynchronous call can be implemented using delegates?

758


Explain the basic functionality of garbage collector?

714


What is the significance of attaching a profile while creating a user?

753


Explain the various authentication mechanisms in asp.net.

697