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.
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.