Why does string not require a new like other reference types?

  • Thread starter Thread starter Ray Cassick
  • Start date Start date
R

Ray Cassick

I can't believe why I had not noticed this before and why I never asked it
before...

When defining a string why is it not required to use the new keyword? Like:

Dim a As String = New String

You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Seem odd to me that all the other reference types seem to require a new to
create the instance portion of the type and String does not.

Thanks for any insight...
 
Ray said:
I can't believe why I had not noticed this before and why I never asked it
before...

When defining a string why is it not required to use the new keyword? Like:

Dim a As String = New String

You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Seem odd to me that all the other reference types seem to require a new to
create the instance portion of the type and String does not.

Thanks for any insight...

If you declare a string without assigning a value to it, you haven't
created any instance of the string class, you have only declared a
reference.

All literal string values are created as constants in the assembly. As
they already exists when the code is started, you don't need to use the
new keyword when assigning them.
 
Ray Cassick said:
When defining a string why is it not required to use the new keyword?
Like:

Dim a As String = New String

Because it would not make sense. Strings are immutable in .NET. If you
write 'Dim a As String = "Hello World"' you are creating an instance of
'String' containing "Hello World" and assign a reference to it to the
variable 'a'. Each assignment to a 'String' variable changes the reference.
You can do it when using the string type with some arguments in the
constructor but it look like there is no empty constructor on the String
class.

Which value should a string constructed with the parameterless constructor
have?
 
Herfried K. Wagner said:
Because it would not make sense. Strings are immutable in .NET. If you
write 'Dim a As String = "Hello World"' you are creating an instance of
'String' containing "Hello World" and assign a reference to it to the
variable 'a'. Each assignment to a 'String' variable changes the
reference.


Which value should a string constructed with the parameterless constructor
have?

Thanks for the response...

So as I understood it (and the way it was actually taught to me) is this.

When you do this:

Dim a As TypeName

The storage is declared on the managed stack. IF the type is a ValueType
then nothing else is needed and the storage is all done on the stack. IF the
type is a reference type then the stack storage ends up just being a pointer
that will end up being an address on the heap somewhere but points to
nowhere just yet.

When you do this:

Dim a As ClassName = New ClassName...

THEN the storage is allocated on the stack AND the storage is allocated on
the heap and the stack points to the proper location on the heap.

So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.

I would expect that doing this:

Dim a As String

would simply point to nothing (as it does) just like doing this:

Dim a As Integer

results in a 0.

I am just curious as to why the String type seems to be inconsistent in
behavior with respect to other reference types.
 
Ray,

Your replies sounds to me like somebody driving a car once heard that there
was a carburator in it, and now ask why he cannot find it in his current
car.

It is important for a driver, that a car do exactly as it should do? You are
not the one who designs Net and the way it works.

By the way, playing with words does not work here.

Dim a as integer "results" in a 0
Dim b as string "results" in a ""

Both as with all valuetypes with the lowest value possible.

Strange that you did not mention the Struct DateTime which has almost the
same behaviours

Just my idea reading your replies.

Cor
 
Cor said:
Ray,

Your replies sounds to me like somebody driving a car once heard that
there was a carburator in it, and now ask why he cannot find it in his
current car.

It is important for a driver, that a car do exactly as it should do? You
are not the one who designs Net and the way it works.

By the way, playing with words does not work here.

Dim a as integer "results" in a 0
Dim b as string "results" in a ""

Not at all. Declaring a string without assigning a value either results
in a null reference (for member variables) or an undefined variable (for
local method variables).
Both as with all valuetypes with the lowest value possible.

String is not a value type, so the default value for string is not an
empty string, it's null.
 
"Göran >>
Not at all. Declaring a string without assigning a value either results in
a null reference (for member variables) or an undefined variable (for
local method variables).

I especially placed the double quotes around the words results.

As a string is declared then the type Is String and the object Is Nothing.
As soon as you use a string then it will create that empty string. Even as
that is If string = nothing

:-)

Cor
 
Goran,

By the way, are you using often C#, because this misunderstanding is
typically from people only using that?

Cor
 
So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.

I would expect that doing this:

Dim a As String

would simply point to nothing (as it does) just like doing this:

Dim a As Integer

results in a 0.

I am just curious as to why the String type seems to be inconsistent in
behavior with respect to other reference types.

Strings are an odd case because they are a reference type that is
created by using a language constant. If strings required New, then
every use of a string constant would require New:

Dim a As String = New String("abc") + New String("def")
MessageBox.Show(New String("Some message"))

That's a lot of typing and a lot of clutter for no purpose. Requiring
New provides no functionality over just using the constant by itself.
 
As a string is declared then the type Is String and the object Is Nothing.
As soon as you use a string then it will create that empty string. Even as
that is If string = nothing

:-)

Cor

That is not a true statement. Consider the following trivial example.

Dim a As String
a.ToString()

The example uses "a" before it is assigned and that generates a
NullReferenceException as you would expect from any reference type.
An empty string did not get created and assigned to "a" at any point.
 
Brian,

You are right, however I seldom set a string to a string.

This works
Dim a As String
TextBox1.Text = a

Cor
 
Brian,

You are right, however I seldom set a string to a string.

I'm not sure what you mean by that.
This works
Dim a As String
TextBox1.Text = a

Cor

That works because TextBox.Text checks to see if the value being
assigned is a null reference first and if it is then it creates an
empty string and assigns that instead (I checked using Reflector).
That behavior is specific to TextBox (and perhaps other classes in the
BCL as well) and cannot be generalized to every use of a string.
 
I'm not sure what you mean by that.


That works because TextBox.Text checks to see if the value being
assigned is a null reference first and if it is then it creates an
empty string and assigns that instead (I checked using Reflector).
That behavior is specific to TextBox (and perhaps other classes in the
BCL as well) and cannot be generalized to every use of a string.

Another oddity of strings is that test for equality with an empty
string doesn't trap if the string reference is Nothing, but any other
use of the reference to Nothing does trap.

Dim a As String = Nothing
If a = "" Then ' This doesn't trap, unlike other reference types
If a.Length > 0 Then ' This does trap like other reference types

I'm not sure why it works this way. While this behavior can be
convenient (saving a test for Is Nothing), it only helps when
comparing for the empty string.

It's another example of how String is treated as a cross between
reference and value types.
 
Jack said:
Strings are an odd case because they are a reference type that is
created by using a language constant. If strings required New, then
every use of a string constant would require New:

Dim a As String = New String("abc") + New String("def")
MessageBox.Show(New String("Some message"))

That's a lot of typing and a lot of clutter for no purpose. Requiring
New provides no functionality over just using the constant by itself.

Besides, when you use a literal string it's not created when you use it.
It already exists as a constant in the assembly, so if New was requiered
like that, the compiler would actually replace each New call with the
reference to the string constant.

You can verify that using a string literal doesn't create any new object
like this:

Dim x(1) As String
For i As Integer = 0 to 1
x(i) = "asdf"
Next
Console.WriteLine(Object.ReferenceEquals(x(0), x(1)).ToString())

It will write out True, as the code in the loop assigns the reference of
the same string literal to both the items in the array.

You can also verify that string literals are reused like this:

Console.WriteLine(Object.ReferenceEquals("asdf", "asdf").ToString())

It will write out True, as the same string constant is used for both
string literals.
 
Cor said:
Goran,

By the way, are you using often C#, because this misunderstanding is
typically from people only using that?

Cor

I am indeed "using often C#", but I have also used a lot of other
languages, inlcuding some flavours of assembly language and machine
code, so I have a pretty good sense for what's really happening to the
code that I write.

Do you use mostly Visual Basic? Misunderstandings like that is typical
for people only using that.

;)
 
Cor said:
"Göran >>


I especially placed the double quotes around the words results.

That doesn't matter. Neither the result or the "result" is a string.
As a string is declared then the type Is String and the object Is Nothing.
As soon as you use a string then it will create that empty string. Even as
that is If string = nothing

No, it doesn't work that way. A string reference does not automatically
change into something else just because you use it. If it would, you
could never get a null reference exception when using a string.
 
Another oddity of strings is that test for equality with an empty
string doesn't trap if the string reference is Nothing, but any other
use of the reference to Nothing does trap.

Dim a As String = Nothing
If a = "" Then  ' This doesn't trap, unlike other reference types
If a.Length > 0 Then ' This does trap like other reference types

I'm not sure why it works this way.  While this behavior can be
convenient (saving a test for Is Nothing), it only helps when
comparing for the empty string.

It's another example of how String is treated as a cross between
reference and value types

The first example demonstrates how the VB compiler injects a call to
Microsoft.VisualBasic.CompilerServices.Operators.CompareString. That
method treats null strings the same as empty strings. It is
interesting to note that the C# equivalent of that example results in
a completely different outcome.
 
Ray Cassick said:
So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.

I would expect that doing this:

Dim a As String

would simply point to nothing (as it does) just like doing this:

Dim a As Integer

results in a 0.

Well, 'Integer' is a value type, 'String' is a reference type. Thus the
value of 'a' in the sample above is a 'Nothing' reference.
 
I am asking because I wanted to explain the difference between these two
types to someone else and could not come up with a real reason (other than -
it just works that way with strings) for them. Right, I'm not the designer
but that does not stop me form being curious :)

Inconsistencies bug me. It just seemed like something that would pop up in a
Microsoft document somewhere and I have had no luck in locating it. Another
thing that bugs me.

Now that I had the discussion I am ok. Seems more like it was a mixture of
how strings are inherently immutable and how it better to make them work
like ValueTypes in these cases.
 
Ray Cassick said:
The storage is declared on the managed stack. IF the type is a ValueType
then nothing else is needed and the storage is all done on the stack. IF the
type is a reference type then the stack storage ends up just being a pointer
that will end up being an address on the heap somewhere but points to
nowhere just yet.

Pretty much, there are exceptions...
When you do this:

Dim a As ClassName = New ClassName...

THEN the storage is allocated on the stack AND the storage is allocated on
the heap and the stack points to the proper location on the heap.

So, I guess my question is.. if String is a reference type why do you NOT
NEED to allocate it using the New keyword every time? Almost seems like
strings are an odd case with respect to this.

Nope.

This is one of the exceptions to the stack/heap storage option. String
constants results in a reference that points to the code. Just as you'd
do if you were programming in assembly and were assigning a pointer to
a string constant.

So, string constants are pointers to code, and the implementation of
the + and & operators create a new reference and then return that.
 
Back
Top