How do you default a new class to public when creating it in Visual Studio?
In VS2012 it's as easy as going to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
and editing the Class.cs file.
No need to unzip or rebuild cache. A very positive change if you ask me.
VS2015: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(RC): C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(Professional): C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2019 (Enterprise): C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2019 (Professional): C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Starting with Visual Studio 2022 VS is a 64 bit application, meaning the item templates are in C:\Program Files\...
instead of C:\Program Files (x86)
VS2022 (Community): C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2022 (Professional): C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2022 (Enterprise): C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Outdated
VS2019 (Preview): C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
To create a Public class by default for Visual Studio 2012:
Edit this file:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
To look like this:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}
You need to modify the file located in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
.
Visual Studio 2010 (10.0) and below: There is a zip file in there called Class.zip. Unpack this, edit the file to put in your public
keyword then re-pack it (make sure you backup the original).
After this, make sure VS rebuilds it's cache (which is just the zipfiles unzipped into directories in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache
) by opening a Visual Studio command shell and execute the following command:
devenv /installvstemplates
Visual Studio 2012 (11.0) and up: See the answer by @JochemKempe, as it's much easier to change this now, just by editing a single file (no unzipping or rezipping).
UPDATE: Don't forget to open your preferred text editor with admin privileges before you do any edit.