calynr
calynr

Reputation: 1272

C# How to store a string

I want to learn where or how the string value that is written by like "Hello World" is stored.

For Example:

Example 1:

radLabel1.Text = "Hello";
radLabel2.Text = "Hello";
radLabel3.Text = "Hello";

Example 2:

string strTemp = "Hello";
radLabel1.Text = strTemp;
radLabel2.Text = strTemp;
radLabel3.Text = strTemp;

I have known second way is most useful and I am doing like this, but I want to know how the string of Example 1 is stored in RAM. I thought although they are the same, three places are created for them in RAM to store them. But in example 2 only one place is created for the string of Example 2 and using the address of this is accessed. Am I right ? Could you explain this or storing variables in RAM?

Upvotes: 2

Views: 3482

Answers (4)

Hans Passant
Hans Passant

Reputation: 941208

The usual term that's flung around is that string literals are 'interned'. It is a pretty meaningless term and doesn't describe what is really going on that well.

A .NET assembly contains two chunks of data, the metadata and the IL. Metadata is primary used to describe the types in the assembly and contains resources. IL is the code that you wrote, translated to the Intermediate Language format.

The metadata section has 5 tables, one of these is called the "string table". Highly descriptive of what it contains, that's where your "Hello" string is stored. It is already in a format that's identical to the way strings are normally stored in the garbage collected heap, but with an extra flag in the object header that indicates that it is a string literal and not stored in the heap.

The assembly content is mapped into virtual memory by a memory mapped file, exact same animal as the .NET System.IO.MemoryMappedFiles.MemoryMapFile class. The strTemp object reference will be initialized by a single MOV instruction emitted by the jitter and stores a pointer that directly points at the string table entry in mapped view of the file. First time your program actually uses the string content, an operating system page fault ensures that the string will be present in RAM.

The garbage collector will find the strTemp object reference whenever it performs a collection. But will just ignore the reference, the flag in the object header says that it should since the string object isn't actually stored in the garbage collected heap. Which is really what "interning" means.

Upvotes: 6

Poomrokc The 3years
Poomrokc The 3years

Reputation: 1099

When you declared

   radLabel1.Text="Hello";

It creates a temp string with value Hello into somewhere in memory and pass the value to radLabel.text . And for radLabel2.Text and radLabel3.text, the program stil create temp 2 and temp 3 to store "Hello". Beause there is nothing to tell the program that they are the same.

But, when you declared

   string strTemp = "Hello";

It creates a string in a memory(of course),but different from "Hello".Because they created a name for it. This work like a desktop shortcut of your computer. So when you use it , you just called the shortcut, and it bypass the progress of creating a new string because the value is already defined.

Hope you understand

Upvotes: 0

nphx
nphx

Reputation: 1427

In Example 2, you are creating a single string on the first line. Then you're just passing a reference to it to those 3 labels. As you have said, it's still a single object in the memory. However, since strings are immutable in C#, assigning a new string to radLabel2.Text won't change the value of the others, but will instead create a new string.

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26189

I thought although they are the same, three places are created for them in RAM to store them.

Strings are interned

when they are same string constants only one copy is maintained.That Means in your first example there is only one "Hello".

Upvotes: 0

Related Questions