Creating Public Classes In Visual Studio

Recently, I came across a silly mistake where I was not able to see my newly created class IntelliSense in Visual Studio 2015/2017.

After starting from scratch, it came to my notice that VS creates a class by default, without access specifiers and that is the reason it didn't show up in IntelliSense.

You can fix it permanently by modifying the item template for the file type ‘Class’.

Note that making classes public would expose classes to other assemblies and you need to take care of making them private if you need to. It really depends on whether you will need to work with most of the classes being public or private in current project. You can change it back anytime.

Here is how.

When you try creating a new class, it uses the template located under the following path:

VisualStudioInstallationDirectory\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

You should see the file named "Class.cs" which has content as shown below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. $if$($targetframeworkversion$ >= 3.5) using System.Linq;  
  4. $endif$using System.Text;  
  5. $if$($targetframeworkversion$ >= 4.5) using System.Threading.Tasks;  
  6. $endif$  
  7. namespace $rootnamespace$  
  8. {  
  9.     class $safeitemrootname$ {}  
  10. }  
Here, change the class definition by adding public access specifier and save/override the file.

public class $safeitemrootname$

You should be good to go. Now, if you add the class to your project, it will be public by default.

Please leave your comments for any questions/concerns.