Reputation: 29520
When creating a new method in a class in Visual Studio, the IDE generates the method as internal
, but I want public
to be the default.
I didn't find a way to change this behavior. I know that there are templates for new files and classes but that does not help me because I need it for methods.
Upvotes: 3
Views: 90
Reputation: 3321
The default access modifiers for classes at the namespace level is internal
, whereas the default access modifier for class members (including nested classes) is private. There isn't a direct setting in Visual Studio to change this default behavior to public
. However, as @Karen Payne said in above comment, you can customize Code snippets for methods.
Here are detailed steps:(e.g: Visual Studio 2022)
1.Navigate to C:\Program Files\Microsoft Visual Studio\2022\your version\VC#\Snippets\1033\Visual C#
.
2.Create a file named method.snippet
.Copy other .snippet
file as a demo and insert the following:
<Snippet>
<Declarations>
<Literal>
<ID>methodname</ID>
<ToolTip>Method name</ToolTip>
<Function>MethodName()</Function>
<Default>TestMethod</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public void $methodname$ (){
$end$
}]]>
</Code>
</Snippet>
3.Type method
and Tab
key, the snippets for public method are generated automatically.
public void TestMethod()
{
}
You can also raise a feature request at Developer Community (visualstudio.com). That will allow you to directly interact with the appropriate product group, and make it more convenient for the product group to collect and categorize your suggestions.
Upvotes: 0
Reputation: 39
AFAIK Visual Studio doesn’t provide an option to change the default access modifier for methods generated by the IDE (e.g., through code generation or refactoring tools). By default, it uses internal for new methods in C# because that aligns with the language's conventions.
Might be wrong though
Upvotes: 0