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
Year | Edition | Absolute path |
---|---|---|
2022 | Community | %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
Is There a Generic Constructor with Parameter Constraint in C#
Xaml Binding Not Working on Dependency Property
Dot Character '.' in MVC Web API 2 for Request Such as API/People/Staff.45287
When Is a Static Constructor Called in C#
Shorter Syntax for Casting from a List<X> to a List<Y>
How to Get a Property Value Based on the Name
How to Dynamically Create ASP.NET Controls Within Dynamically Created ASP.NET Controls
Writing to a Textbox from Another Thread
Testing If Object Is of Generic Type in C#
Formatting a Float to 2 Decimal Places
Why Is a Dictionary "Not Ordered"
How to Make an Event in the Usercontrol and Have It Handled in the Main Form
Get All Derived Types of a Type
Validate Image from File in C#
Reading Datetime Value from Excel Sheet
Stop Visual Studio Debug Putting Slash in String Containing Double Quotes