0

I recently moved over to C# from Java and wanted to know how do we explicitly define a string thats stored on heap.

For example:

In Java, there are two ways we can define Strings:

String s = "Hello" //Goes on string pool and is interned
String s1 = new String("Hello") //creates a new string on heap

AFAIK, C# has only one way of defining String:

String s = "Hello" // Goes on heap and is interned

Is there a way I can force this string to be created on heap, like we do in Java using new operator? There is no business need for me to do this, its just for my understanding.

3
  • Interned strings don't "go on the stack" in any sense of the word. In both languages (and many others), strings are always reference types. Commented Mar 26, 2014 at 11:28
  • 1
    FYI in Java String s = "Hello" doesn't gets stored on stack, they get stored on a special heap space called String pool Commented Mar 26, 2014 at 11:30
  • Thanks, delnan and sanbhat. I have updated the question and corrected that. Commented Mar 26, 2014 at 11:32

5 Answers 5

1

In C#, strings are ALWAYS created on the heap. Constant strings are also (by default) always interned.

You can force a non-constant string to be interned using string.Intern(), as the following code demonstrates:

string a1 = "TE";
string a2 = "ST";
string a = a1 + a2;

if (string.IsInterned(a) != null)
    Console.WriteLine("a was interned");
else
    Console.WriteLine("a was not interned");

string.Intern(a);

if (string.IsInterned(a) != null)
    Console.WriteLine("a was interned");
else
    Console.WriteLine("a was not interned");
Sign up to request clarification or add additional context in comments.

3 Comments

String constants are interned. Strings built during execution are not.
(by default) always? I don't understand, either they're always interned or you can turn the setting off. How can it be both ways?
@AshBurlaczenko I meant that unless you take some specific action, strings will always be interned - but there's a way of turning it off.
1

In C#, the datatypes can be either

  1. value types - which gets created in the stack (e.g. int, struct )
  2. reference type - which gets created in the heap (e.g string, class)


Since strings are reference types and it always gets created in a heap.

1 Comment

"Created on the stack" is an awful characterization of value types, for many reasons discussed by Eric Lippert's The Truth About Value Types.
0

In the .net platform, strings are created on the heap always. If you want to edit a string stay: string foo = "abc"; string foo = "abc"+ "efg"; it will create a new string, it WON'T EDIT the previous one. The previous one will be deleted from the heap. But, to conclude, it will always be created on the heap.

1 Comment

The part about strings on the stack was removed from the question.
0

Like Java:

char[] letters = { 'A', 'B', 'C' }; 

string alphabet = new string(letters);

and various ways are explained in this link.

Comments

0

On .Net your literal string will be created on the heap and a reference added to the intern pool before the program starts.

Allocating a new string on the heap occurs at runtime if you do something dynamic like concatenating two variables:

String s = string1 + string2;

See: http://msdn.microsoft.com/library/system.string.intern.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.