When you create a new class in C# from Visual Studio, are you often annoyed that the class is created by default without a public access modifier? A class that does not have an explicitly defined access modifier has internal access. However, if you want your class to be public, you must always add the public keyword after creating it. This is a bit unnecessary, it would be nice if classes were public by default. Is it even possible to do this? If so, how to do it? Why are classes marked as internal by default? In this short article I will answer all these questions.
What does the default class created in Visual Studio look like?
By default, after creating a class from Visual Studio, it looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App
{
class Post
{
}
}
The class is created from a previously defined skeleton, which we can adapt to our needs.
Why are created classes marked as internal by default?
The fact that the class is created as internal is also not accidental. Due to the fact that it is a class with the internal modifier, it is not shared externally, so this procedure is safe. In C#, not only classes, but also new fields and methods are created by default with the minimum required scope.
How to set new classes in visual studio to be created with the public modifier by default?
To change the default class skeleton, you need to go to the Class.cs file, which, depending on the version of Visual Studio you are using, may be in a different folder.
In Visual Studio 2019 Community it is located in:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
Visual Studio 2019 Enterprise:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
Visual Studio 2019 Professional:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
This is what the Class.cs file looks like:
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$
{
class $safeitemrootname$
{
}
}
You can modify it as you wish, if you want new classes to be created with the public access modifier, change the content of the Class.cs file to:
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$
{
}
}
From now on, classes created will be public by default.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculateYear
{
public class Post
{
}
}
SUMMARY:
If you are tired of adding the public access modifier to a class every time, be sure to change the template for newly created classes in Visual Studio. As you can see, it's not complicated and can save you a lot of time in the long run. If you have a problem and this template doesn't work for you, let me know in the comment, I will definitely help you.