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

Explain how viewstate is being formed?

550


Is there any property names “isnavigating”?

583


Explain the Order of events in an asp.net page?

610


What is webresource axd?

474


What is intrinsic objects in asp.net?

656






What is the difference between session.abandon() vs clear()?

553


What are the Types of authentications in IIS

605


How can we register exception filter from the action?

673


calling result set one procedure to anothar procedure in sql2000

1350


How to bind all the binding controls in a page at once in ASP.NET?

537


How to handle errors in Web API?

637


What is owin authentication?

528


What are the events in a page life cycle?

543


Which is the best institute to learn Microsoft Technologies and the faculty if you Know?

1622


Which library is used by the testers and developers to develop automated tests and create testing tools?

556