Skip to main content

The String is both a reference type and a value type in .NET

In .NET the string class is a special type. It is a hybrid of both a value
type and a reference type.

A typical reference type is stored on the heap, and a typical value type is
stored on the stack.
Classes are reference types, for example, Windows.Forms.Textbox
Numbers are value types, for example, Integer and Double.

Value types are able to be stored on the stack because they have a finite
length. Reference types have variable length. So for reference types, .NET
keeps a memory reference on the stack to a location on the heap where the
actual value is stored. This allows the stack to be very fast and compact
while still being able to store large values like a string variable that
contains the preamble to the Constitution.

An analogy: A comparison to Sqlserver variables is the difference betwnee
the char and the varchar data types. The char always has a fixed length, the
varchar does not.

So the string is a special animal. You can use it without always having to
use a constructor. Good thing, it would be a real pain to have to write:
String myStringVar1 = new("hello ");
String myStringVar2 = new("world");
String myStringVar3 = myStringVar1 + myStringVar2;

Instead, because string is a special case in .NET we can just write:
String myStringVar3 = "hello " + "world";

Comments

Unknown said…
The string class is not a hybrid of both value and reference types. It is a reference type through-and-true. It is only special in the sense it is one of the few reference types classified as a Primitive Type. Because it is a Primitive Type, the compiler allows for special syntax when working with Primitive Types.

Going with your examples:
int myIntVar1 = new int(5);
But as you stated, there is simpler syntax:
int myIntVar1 = 5;

The important thing to understand is the syntax applies to value types and reference types because they are Primitive Types.
Coach Phil said…
Thanks to you both for posting.
I'm studying for a .NET exam and I'm confused. I'm hoping one of you can clarify for me.

If I understand reference types correctly one reference type variable to another and change one of them - the other changes as well - as each references the same pointer to the heap. Yes?

IE:

string sTemp1 = "test";
string sTemp2 = sTemp1;
sTemp2 = "xxx";

Does stemp1 now = "xxx"? This is not my experience.

Does immutability factor into this somehow? Does immutability somehow give it the flabor of a value type?

Thanks,
Adam
Coach Phil said…
post to ensure I get emailed. Thx.
Phil, I hope you got your answer before now, but yes, that is one of the ways that a string is treated differently than normal reference types, and that is why I disagree with Tim. When you pass a string into a method, the value is copied and the copy is passed into the method.

Popular posts from this blog

How to Create and Run Tableau Bridge on Linux Containers

Tableau Bridge is now availble on Linux Containers. Yay! Now what does this mean and how do I build and run Linux Containers? We will discuss the advantages of running Bridge on Linux Containers the steps to build them, and finally, we will provide some automation script ideas for monitoring and scaling Linux Bridge agents. Tableau Bridge Today Until recently, Tableau Bridge was only available as a Windows application running on a Windows VM. It supported only one bridge agent per Virtual or Physical Machine. Advantages of Bridge in Containers Better Hardware Utilization: Linux containers are more efficient than Windows VMs, requiring only about 1/50th of the disk space. Ability to Spin Up Multiple Bridge Agents: With Linux Containers, it becomes easier to spin up multiple bridge agents on a single machine, improving scalability and resource utilization. Infrastructure Automation: Linux Containers enable easier automation of provisioning bridge agents and upgrading Tableau Bridge, the...

RAM Disks do not speed up Visual Studio

  The limiting factor for Visual Studio is disk IO. I got a tip to speed up Visual Studio from Channel 9 by creating a RAM disk which sounded like a great idea. However, when I ran a thorough set of tests, I found that the performance difference between the Ram disk and the hard disk were not appreciably different. This was a big surprise since RAM is 240,000 times faster than disk (see my previous blog post). But the reason is because Visual Studio and Vista do a lot of caching. So compile times for the same project in RAM disk and on hard disk were pretty similar. I also tested the time it took to search the entire solution for a word, and times to open a solution. There was no discernable difference!   If you still want to try it out and create your own RAM disk, you can download a simple RAMDISK.EXE utility to create a RAM disk in just a few minutes. What is a RAM Disk ?   Ramdisk is a virtual drive created in RAM.   Performance Analysis Creating f...

SpreadsheetGear vs. SyncFusion vs. ComponentOne

I conducted a three month analysis comparing three top spreadsheet controls for .NET application developers: SpreadsheetGear , SyncFusion Spreadsheet , and ComponentOne Spread.NET . The definite winner was SpreadsheetGear, with ComponentOne Spread.net in second. SpreadsheetGear provides the most Excel-like experience, is the most performant, and provides the most responsive and capable product support. I build a reporting engine for my company and SpreadsheetGear made it awesome. Our customers have been very pleased. I wanted to share my extensive analysis with any other developers out there trying to find the right spreadsheet control for their .NET application.   Comparison Spreadsheet gear grid goes to 1 million! Wow. (SyncFusion goes to 500 by default, ComponentOne goes to 60k) SpreadsheetGear is an older control which has been around for a while. It is built by the same guys who built Formula1, so they have been doing spreadsheets for many years. Sp...