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?
-
1You need to change the class template, [as described here](http://web.archive.org/web/20090403082555/http://dotnet.org.za/hiltong/archive/2008/02/26/amending-the-new-class-visual-studio-c-template.aspx). It's slightly more involved than just editing a single file (which is what you might expect) but it doesn't sound too bad. – Jon Skeet Mar 31 '09 at 05:23
10 Answers
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

- 26,385
- 18
- 89
- 135

- 2,566
- 2
- 16
- 12
-
2This doesn't seem to be working for me. I even replaced the Class.cs file in the ItemTemplatesCache directory as well. Rebooted VS2012 each time. Is there a way to rebuild the cache maybe? – crush Jul 16 '13 at 15:44
-
-
7For VS2013 use this location: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class – Zar Shardan Dec 07 '13 at 18:13
-
1This worked fine for me. I just did a "Clean Solution" without restarting VS 2012. Added a new class file and bingo-bango, there was my new modifier. By the way, I also always make them public partial, rather than just public. – silverfox1948 Mar 26 '14 at 21:00
-
10You may need to have your text editor open in Admin mode, in order to update the actual class template. – david.barkhuizen Dec 17 '14 at 13:56
-
3For VS2015 use this location: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class However, if you have Xamarin installed, you might also need to do a search on you windows drive for "Class.cs". I ended up just changing all of the results to public. – JochemKempe Sep 03 '15 at 09:39
-
1For Visual Studio Community 2017 I found it here: `C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs` – Sam White May 07 '18 at 19:51
-
For Visual Studio 2019 Preview: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs` – Simon C Dec 20 '18 at 10:22
-
2Visual Studio 2019 is out now so Preview should change to indicate the version. I only have Professional installed but I guess the other two would be Community and Enterprise VS2019 Professional: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs` – Dave Thompson May 23 '19 at 19:09
-
Visual Studio 2019 Enterprise location is: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class` – Victor Procure Jul 17 '19 at 14:04
-
1VS2022 location is: `C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs` – ScubaSteve Dec 09 '21 at 16:00
-
It's worth noting that when you open the class.cs file, you wont see `internal`. It confused me at first because I thought it wasn't the right file. But I added public and it worked. – Michael Wagner Sep 10 '22 at 20:19
-
In Visual Studio 2022, if you want to change the class template for an AspNetCore project, you need to change the template in `C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs`. No need to restart, but you need to edit in Administrator mode. – HHansen Jan 24 '23 at 15:41
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.

- 1
- 1

- 2,095
- 13
- 19
-
Big Thank you. On a 64 bit Windows 7 Machine with VS 2012 this path is at C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp – Sudhanshu Mishra Jul 22 '13 at 12:49
-
what if you want change the item template between a team on different machines, any ideas? – DevDave Jul 31 '13 at 17:45
-
In VS2013, you'll need to run that Developer Command Prompt for VS2013 as Administrator, and run the command after closing Visual Studio - it won't complete while VS is open. – Chris Moschini Dec 13 '13 at 07:28
-
1No need to visit the blog post @Guy referenced, as the post has been edited to include the information found there. If you *want* to visit that blog post, it has now moved to a new URL: http://www.guyellisrocks.com/2009/03/defaulting-class-to-public-from-visual.html – ErikE Jul 21 '15 at 16:54
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$
{
}
}

- 4,103
- 5
- 27
- 37

- 6,604
- 8
- 56
- 61
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

- 1
- 1

- 10,804
- 5
- 49
- 61
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

- 4,704
- 13
- 34
- 52

- 101
- 1
- 3
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:
- MSDN on VS Project Templates
- Create your own VS Project Templates
- CodeProject on VS Project Templates
Marc

- 732,580
- 175
- 1,330
- 1,459
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 $_
}

- 31,051
- 11
- 110
- 173
-
My path was `..\2017\Professional\...` and I edited it manually but this showed the new folder structure for 2017. Thanks. – ste-fu Jul 25 '17 at 08:39
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.)

- 1,102
- 1
- 11
- 19
In Visual Studio 2022 you can go to this location and edit the Class.cs file.
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

- 431
- 1
- 6
- 17
The Everything program is very nice for finding files on the computer.
I used this filter to find all the Class.cs files
CSharp\Code\1033\Class\Class.cs
Just putting this out there because finding the files seems like a painful part of solving this problem.
So you can use the Everything program to find the file and then edit it as suggested elsewhere.

- 7,615
- 5
- 33
- 58