1

Considering:

void Update(){
    Method("String");
}

I would like confirmation that this is creating a new string each round and then that string is referred by a new variable placed on the stack.

Then is this better:

string param = "String";
void Update(){
    Method(param);
}

I would think this avoid the creation of the new string each round and it is always the same that gets referred to by the stack variable. Also, keeping in mind that the string is only read and not modified.

Sam
  • 7,252
  • 16
  • 46
  • 65
Everts
  • 10,408
  • 2
  • 34
  • 45
  • So... you want to verify that the "param" variable is always a static allocation? –  Jun 29 '14 at 16:23
  • Just to know if the second example is better than the first for avoiding a new string creation each update calls. – Everts Jun 29 '14 at 16:25
  • 2
    possible duplicate of [String interning in .Net Framework - What are the benefits and when to use interning](http://stackoverflow.com/questions/8054471/string-interning-in-net-framework-what-are-the-benefits-and-when-to-use-inter) – GSerg Jun 29 '14 at 16:27
  • Strings are immutable in C#, so obviously it cannot be modified. – Brian Rasmussen Jun 29 '14 at 16:46

4 Answers4

3
void Update(){
    Method("String");
}

In this example "String" will be interned once and there will be only one object in memory.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
  • But as the Update is called each frame (maybe I should have mentioned that) the parameter will be a new one next round isn't it? – Everts Jun 29 '14 at 16:26
  • 1
    when Method will be executing, there will be another reference to same object. – Kirill Bestemyanov Jun 29 '14 at 16:30
  • So, the compiler will keep the string in memory and only the reference is new meaning the second example does not add much to the program. – Everts Jun 29 '14 at 16:33
1

String literals are stored in an internal pool by the c# compiler, one per assembly, and reused as required. For documentation, see the String.Intern Method

The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

More here. You can convince yourself that your string is stored internally with String.IsInterned.

dbc
  • 104,963
  • 20
  • 228
  • 340
1

Try it yourself:

string oldS;
int count;

void Method(string s)
{
    if (ReferenceEquals(oldS, s)) // compare by reference
    {
        count++;
    }

    oldS = s;
}

public void Test()
{
    for (var i = 0; i < 100; i++)
    {
        Method("string");
    }

    Console.WriteLine(count);
}

Run the above code and you can see that output is 99 (the first time isn't counted). Maybe this make you feel safer ;)

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
0
void Update(){
Method("String");
}

Definitely there will be one object in the memory,

There are three things you need to know in order to predict what will happen if you had chosen this,

string param = "String";
void Update(){
Method(param);
}

1.Strings are reference types in C#. But this is only part of the picture.

2.They are also immutable, so any time you do something that looks like you're changing the string, you aren't. A completely new string gets created, the reference is pointed at it, and the old one gets thrown away.

3.Even though strings are reference types, strMain isn't passed by reference. It's a reference type, but the reference is being passed by value.

Monitor
  • 342
  • 2
  • 13