By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

Parameterless String constructor

P: n/a
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.

Oct 5 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Oct 5, 4:29 pm, dippyk...@gmail.com wrote:
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.

Oct 5 '07 #2

P: n/a
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.

Oct 5 '07 #3

P: n/a
On Oct 5, 4:53 pm, dippyk...@gmail.com wrote:
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

Oct 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.