Marking A Class Static in VB.NET

前端 未结 5 1706
你的背包
你的背包 2020-12-01 10:12

As just stated in a recent question and answer, you can\'t inherit from a static class. How does one enforce the rules that go along with static classes inside VB.NET? Sin

5条回答
  •  遥遥无期
    2020-12-01 10:37

    Almost there. You've got to prevent instantiation, too.

    NotInheritable Class MyStaticClass
    
        ''' 
        ''' Prevent instantiation.
        ''' 
        Private Sub New()
    
        End Sub
    
        Public Shared Function MyMethod() As String
    
        End Function
    
    End Class
    
    • Shared is like method of static class.
    • NotInheritable is like sealed.
    • Private New is like static class can not be instantiated.

    See:
    MSDN - Static Classes and Static Class Members

提交回复
热议问题