Parameterless String constructor

  • Thread starter Thread starter dippykdog
  • Start date Start date
D

dippykdog

Does anyone know why there isn't a parameterless constructor for the
String type?

Given that you can...

Dim s as New String("Hello") ' inefficient, don't do this

I guess I would have expected...

Dim x as New String()

to give me x pointing the an empty string.
For what it's worth, the reason I came across this was because I was
trying to do this in a generic-based class...

Class XYZ(Of T)
Public Sub New()
If Not GetType(T).IsValueType Then
Dim ci As ConstructorInfo =
GetType(T).GetConstructor(System.Type.EmptyTypes)
Dim obj As T = DirectCast(ci.Invoke(Nothing), T)
Me.SetValue(obj)
End If
End Sub

And when I instantiated one of these of type String, I got the famous
NullReferenceException because ci remained Nothing. The reason: String
has no constructor. I've sinced created a new exception for this class
to throw in such a case, but I still found it odd about String not
having a constructor without any arguments. I'm sure there's probably
a reason; I'm just not seeing it.
 
Does anyone know why there isn't a parameterless constructor for the
String type?

Given that you can...

Dim s as New String("Hello") ' inefficient, don't do this

I guess I would have expected...

Dim x as New String()

to give me x pointing the an empty string.
For what it's worth, the reason I came across this was because I was
trying to do this in a generic-based class...

Class XYZ(Of T)
Public Sub New()
If Not GetType(T).IsValueType Then
Dim ci As ConstructorInfo =
GetType(T).GetConstructor(System.Type.EmptyTypes)
Dim obj As T = DirectCast(ci.Invoke(Nothing), T)
Me.SetValue(obj)
End If
End Sub

And when I instantiated one of these of type String, I got the famous
NullReferenceException because ci remained Nothing. The reason: String
has no constructor. I've sinced created a new exception for this class
to throw in such a case, but I still found it odd about String not
having a constructor without any arguments. I'm sure there's probably
a reason; I'm just not seeing it.

I believe it has to do with the String type being immutable, that is
it acts more like a value type than a reference type. Each time you
change a strings value, you actually create a new string, you don't
change the value of the old one.
 
Thanks. You're a good man. (ha)
Interestingly, whereas this doesn't work...

Dim x As String
x = New String()

this does...

Dim x as String
x = New String(Nothing)

and x becomes equal to "" (empty string)

There is *SO* much I don't understand.
 
Thanks. You're a good man. (ha)
Interestingly, whereas this doesn't work...

Dim x As String
x = New String()

this does...

Dim x as String
x = New String(Nothing)

and x becomes equal to "" (empty string)

There is *SO* much I don't understand.

I always use "Dim x As String = String.Empty"

Thanks,

Seth Rowe
 
Does anyone know why there isn't a parameterless constructor for the
String type?

Given that you can...

Dim s as New String("Hello") ' inefficient, don't do this

I guess I would have expected...

Dim x as New String()

to give me x pointing the an empty string.

Note that instances of 'String' are immutable. An empty string can be
obtained using 'String.Empty'. In addition, make sure you read the
documentation on the 'String' class and string interning.
 
Back
Top