2

I have a question about string class. System.String is a class type, but why we can use it without instantiate it with New keyword?

For example:

Dim CommandLineParameters As String = Microsoft.VisualBasic.Interaction.Command

No new keyword being used at all

user774411
  • 1,749
  • 6
  • 28
  • 47
  • 1
    The System.String class has several mysteries, but this is not one of them. The assignment statement simply copies a *reference* to the string returned by the Command property getter. Standard reference type behavior. Think of it as the address on a postcard, getting to the same mailbox. With a hard guarantee that nobody is ever going to move the mailbox. Called "immutability" in programming terms. – Hans Passant Sep 03 '11 at 22:26
  • @Hans Passant: Please share with me some System.String mysteries... – user774411 Sep 03 '11 at 22:30

4 Answers4

3

When you write

Dim s As String = "hello"

you are simply assigning a reference. Behind the scenes the compiler generates code to instantiate the string object that holds "hello". That instantiation probably happens long before the assignment but the full details of that are another story.

In summary, the code above does not instantiate an object.

Likewise, in your example:

Dim CommandLineParameters As String = Microsoft.VisualBasic.Interaction.Command

no object is instantiated. All that happens is that a reference to an existing object is copied.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

You can't really use it without instantiating (with some caveats written below). You can:

A) Copy a string reference to another string reference

or

B) Add a string to a Nothing value. This has been added to simplify the using of strings. See for example C#: Why is adding null to a string legal? but it should be the same for VB.NET .

BUT

C) In VB.NET there is a big difference with C#: "internal" VB.NET functions consider Nothing to be equivalent of "". So Len(Nothing) = 0 and UCase(Nothing) = "". Methods of the string class will still throw an exception if used on Nothing (from String Manipulation and Nothing = String.Empty (Why are these equal?))

I'll add that string literals (for example "Foo") are pre-built during the loading of the assembly where they are defined. So if you do

Dim aString As String = "A String"

what you are doing is copying the reference of the string object containing "A String" to aString.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Huh? I can do `Dim CommandLineParameters As String = Microsoft.VisualBasic.Interaction.Command`. No new keyword being used at all – user774411 Sep 03 '11 at 20:33
  • Your answer is quite unclear. Assignment doesn't copy the `string` object, it just copies the reference. – svick Sep 03 '11 at 20:47
  • @ Dee Jay that's an example of copying a reference to a string you're not instantiating. – Rune FS Sep 03 '11 at 21:46
1

Yes, the compiler has some shortcuts that let you use strings without explicitly creating them.

When you use a literal string, it's not even created when you use it, instead all literal strings are created when the application starts, so you only copy the reference of an already existing string.

Your example is similar, you are not creating a new string, you are just copying the reference of a string that already exists.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
-1

You can.

char[] chars = { 'w', 'o', 'r', 'd' };
string string1 = new string(chars);

Also

string my string = "test";

that instantiates as new string with the value "test" What you are talking about is accessing Static Type members, such as String.Format or String.Join.

msarchet
  • 15,104
  • 2
  • 43
  • 66
  • the second line doesn't instantiate a new object it will simply use a reference to an already instantiated object. – Rune FS Sep 03 '11 at 21:46
  • @RuneFS it does instantiate a new object, unless an instance of the string class exists where a the instance matches "test" exists. The JIT takes care of the instantiating – msarchet Sep 04 '11 at 02:55
  • 1
    @msarchet: If you think of it, it doesn't make sense to check if the literal string exists, because then you would need to already have the string that you are looking for. All literal strings are created when the application starts, and assigning a literal string just copies an already known reference. – Guffa Sep 04 '11 at 08:44
  • @Guffa, which is done by the JIT when the application starts? Then it just uses the string pool to reference the existing instance if it finds one, correct? – msarchet Sep 04 '11 at 15:02
  • @msarchet: No, the code doesn't find a string because it's not even looking for one. The JIT compiler already knows where the string is, so the reference is placed directly in the code. – Guffa Sep 04 '11 at 15:30