Partager via


Req14: Non-empty default partial methods

[This post is part of a series, "wish-list for future versions of VB"]

 

IDEA: Allow a non-empty default partial method. (This suggestion from Bill McCarthy). For instance,

Partial Class C

    Partial Private Sub f(ByVal s As String)

        Console.WriteLine("default body - {0}", s)

    End Sub

End Class

Partial Class C

    Private Sub f(ByVal s As String)

        Console.WriteLine("user provides a body")

        Partial("and can also call the default body if desired")

    End Sub

End Class

 

Provisional evaluation from VB team: Seems an okay idea, but niche, and confusing. Does anyone have scenarios where this would be useful?

Comments

  • Anonymous
    February 15, 2010
    I can’t think of a good use for this...

  • Anonymous
    February 15, 2010
    Brilliant. Think code generation. Partial classes changed the way we do code generation. Before, we generated base classes and a derived (usually empty) class that inherited that one. This way, you could override methods or properties. With the introduction of partial classes, this changed drastically, but we always missed the ability to "override" a method. This way, you could declare all those previously overridable methods as partial and get the same behavior.

  • Anonymous
    February 18, 2010
    While reading this, I thought of the Dispose method that the designer adds to forms, usercontrols, etc heh.

  • Anonymous
    April 14, 2011
    Linq2SQL does its code generation this way in C#.