How to Edit the Visual Studio Templates for New C# Class/Interface

How do I edit the Visual Studio templates for new C# class/interface?


2022

Visual Studio 2022 is now a 64bit process so its location has changed to using Program Files instead.

%ProgramFiles%\Microsoft Visual Studio\<year>\<edition>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs



























YearEditionAbsolute path
2022Community%ProgramFiles%\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Enterprise%ProgramFiles%\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Professional%ProgramFiles%\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

How can I change the default Visual Studio C# new class file template?

You could modify the following file:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

It contains the template used when you add a new class. Inside the same folder you also have the template for interfaces: Interface.zip so that they are public by default. IIRC a restart of VS is necessary to pick the changes.

Change the Implement Interface template

Yes, it is possible to change this template. The C# IDE uses templates for the majority of it's code generation and the IDE. You can update these templates to control the code generation process. They are located in the following directory

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring

Note: The " (X86)" portion will be absent on 32 bit machines.

For this particular situation you want to change the PropertyStub.snippet file.

Visual Studio edit override method template

In Visual Studio under

Tools > Options > Text Editor > C# > Code Style > General

You can change "Use expression body for methods" to never.

Note that this also changes all of the code generation that would have generated expression bodies such as automatically implementing an interface.

Also, if you have a method that you want to convert from an expression body to a block body, or visa versa, simply press Ctrl+. on the method and you will get the quick action to automatically convert it.

How to update Templates?

Inheritance will work for your solution.

  • Create "base" form with all "common" controls
  • Create new "derived" form and change form to inherit from your "base" form.

If you have some common logic in base form, which need to be "overridden" in derived forms - put it to the virtual method

// Base form
protected virtual void Close()
{
// Base logic
}

private void CloseButton_Click(object sender, EventArgs e)
{
Close();
}

// In derived form - just override "Close" method
protected override void Close()
{
// custom logic - will be executed when "Close" button clicked
}

In base form leave empty space for custom controls. Because you will not be able access baseform controls through designer in derived form.

Another approach - Model-View-ViewModel(MVVM)

- Introduce own UserControl with common controls(view) which have property - instance of ViewModel.(Viewmodel will contains behaviour logic and possibility to change "base" settings.)

- Add this user control to all "derived" forms and set UserControl.ViewModelProperty to instance which will represent logic for this particular form.

Without knowing "full" context of your goals - difficult to suggest more, but I am pretty sure you can build maintainable relations between forms, which can share common logic and view.



Related Topics



Leave a reply



Submit