How do you default a new class to public when creating it in Visual Studio?

Visual Studio

Visual Studio Problem Overview


When I right click in the Solution of a C# Visual Studio project and select Add... > Class... it creates a class without a public modifier. How do I get Visual Studio (2008) to default the class to a public class?

Visual Studio Solutions


Solution 1 - 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

Outdated

VS2019 (Preview): C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

Solution 2 - Visual Studio

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.

Solution 3 - Visual Studio

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$
    {
    }
}

Solution 4 - Visual Studio

In VS 2022 Professional, for those who just want to edit the Class.cs file and move on, it's changed paths given that 2022 is 64 bit and is no longer in the x86 path. Now it's:

C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

Solution 5 - Visual Studio

In addition of JochemKempe great answer, here is how to do it for the .NET core templates.


.NET Core

Instead of editing the templates inside the \CSharp\ directory, you need to edit the ones inside \AspNetCore\.

VS2019 (Enterprise): C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs

VS2019 (Professional): C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs

Solution 6 - Visual Studio

You could either create your own project template, or modify the existing one. All these project files are template-driven, so you can alter them and/or add your own.

Check out these links:

Marc

Solution 7 - Visual Studio

Here is a re-entrant PS scriptlet that will update the C# base class Item Template. The path changes depending on which version/edition of Visual Studio you use. It also backups the original template in case you want to revert in the future.

$csharpClassTemplates = @("C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs",
                          "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class\Class.cs",
                          "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs")

$csharpClassTemplates | % {
    Write-Output("Updating template $_")
    Copy-Item $_ -Destination "$_.backup"
    (get-content $_).Replace('public ', '').Replace('class ', 'public class ') | Set-Content $_
 }

Solution 8 - Visual Studio

The answer from JochemKempe works fine, but is a little hard, since you need to rewrite protected files in Program Files.

Only to update the answer. Templates for Community edition are in the folder:

c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates

Another solution

But there is another possibility to create new templates. Copy the files from the source folder to user template folder.

e.g. for Windows Form application it can be one of these source folders (for Visual Studio 2017 Community):

c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\1033\WindowsApplication\
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\1033\WindowsApplication\
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows Root\Windows\1033\WindowsApplication\

The default user template folder is (for Visual Studio 2017 Community):

C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\

And the destination folder for new Windows Form Application template would be:

C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#\Windows\1033\MyWindowsApplication\

With this you will have two "Windows Form Application" when creating new project. The distinction between these two is only in the default file name. To make a better distinction it is possible to change the displayed name of the template. To change the template name, update the .vstemplate file.

The original csWindowsApplication.vstemplate contains line:

    <Name Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2318" />

To change the name you need to change the line to something like this:

    <Name ID="2318">My Windows Form Application</Name>

The negative part of this solution is, that you have to rename your new template and you add a new template to existing templates (the old 'incorrect' example will remain and you can still unwillingly use it).

But there is also a good part. You do not need to be administrator to add or update the example. (There is no need to rewrite files in Program Files folder.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuyView Question on Stackoverflow
Solution 1 - Visual StudioJochemKempeView Answer on Stackoverflow
Solution 2 - Visual StudioMitch DennyView Answer on Stackoverflow
Solution 3 - Visual StudioAaron HoffmanView Answer on Stackoverflow
Solution 4 - Visual StudiokcandersView Answer on Stackoverflow
Solution 5 - Visual StudioJustin LessardView Answer on Stackoverflow
Solution 6 - Visual Studiomarc_sView Answer on Stackoverflow
Solution 7 - Visual StudioSliverNinja - MSFTView Answer on Stackoverflow
Solution 8 - Visual StudioJuloView Answer on Stackoverflow