Automatically generate implementations of base class methods

后端 未结 7 817
我在风中等你
我在风中等你 2021-02-02 07:33

Is there a shortcut of some kind in C# (VS 2008) to automatically implement the virtual and abstract base class methods in a derived class?

相关标签:
7条回答
  • 2021-02-02 08:03

    Don't think this existed when the original question was asked, but at least as of VS 2013, you can automatically create stubs for abstract methods & properties. Just right click on the abstract class name (in your class definition) and pick "Implement Abstract Class". Just like CMS showed with automatically implementing interfaces.

    0 讨论(0)
  • 2021-02-02 08:06

    The current official Microsoft documentation for automatically implementing an abstract base class is here:

    https://docs.microsoft.com/en-us/visualstudio/ide/reference/implement-abstract-class

    0 讨论(0)
  • 2021-02-02 08:10

    For virtual methods type override, give an space and intellisense will show you all methods that can be inherited.

    0 讨论(0)
  • 2021-02-02 08:12

    Just type the Interface that you want to implement, and then click on the Smart Tag, a context menu will popup, and then you can select either Implement Interface or Implement Interface Explicitly:

    All the members to be overridden will be contained within a code region that is named to reflect its purpose.

    All the members will have a line that throws a NotImplementedException.

    0 讨论(0)
  • 2021-02-02 08:15

    For virtual methods, you can type override and then a space. Intellisense should offer you a list of options.

    For abstract methods and properties, you can use the smart tag on the base class or interface (also, Ctrl+. or Shift+Alt+F10 will show the smart tag menu) to generate the concrete items.

    For example, in the following code snippet, you could place the caret at the end of INotifyPropertyChanged and press Ctrl+. to then select Implement Interface, and the PropertyChanged event would be added to MyClass:

    class MyClass : INotifyPropertyChanged
    {
    }
    
    0 讨论(0)
  • 2021-02-02 08:16

    Maybe you want all inheriting/implementing classes to implement a new defined abstract method.

    1. Go to one of the inheriting/implementing classes
    2. use smart tag menu ctrl+.
    3. implement abstract class / interface
    4. watch for options at the bottom of the popup window "Project" or "Solution"
    0 讨论(0)
提交回复
热议问题