这是一个坏主意吗?在公共构造函数中调用泛型私有构造函数是否会创建多个实例,或者这是初始化类变量的有效方法? Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'D
Private Class MyClass Dim _msg As String Sub New(ByVal name As String) Me.New() 'Do stuff End Sub Sub New(ByVal name As String, ByVal age As Integer) Me.New() 'Do stuff End Sub Private Sub New() 'Initializer constructor Me._msg = "Hello StackOverflow" 'Initialize other variables End Sub End Class这是一种有效的方法.有一些警告可以调用新函数:
The Sub New constructor can run only once when a class is created. It
cannot be called explicitly anywhere other than in the first line of
code of another constructor from either the same class or from a
derived class.
在MSDN上阅读有关object lifetime的更多信息.