开发者

Class modifier for automatically generated classes

When I add a new class to a project, the class modifier/access specifier for the newly created class in Visual Studio is ommitted, thereby making it internal.

Is there a wa开发者_JAVA百科y I can specify in Visual Studio settings that whenever I ask for a new class to be created, please make the class public?

I looked in the Tools->Options menu but couldn't find anything. I am guessing it could be in the Templates folder in the My Documents\Visual Studio xxxx folder.


Yes, you can change the file templates (or simply provide secondary file templates), like so. However, before you do that, I would ask myself: does it really take that much effort to add the desired accessibility? I tend to err on the side of the pragmatic when it comes to this... if I wanted significantly different templates, I might do it. Otherwise... meh.


You would have to create your own template to do this, or use code snippets, or perhaps a tool like ReSharper, which has its own snippet mechanism.

However, this is likely to be a bad idea. You will be making things public that do not need to be public. You're better off starting everything as internal, and making them public only as required.


A quick search turned up this:

Prevent Visual Studio from adding default references and usings for new classes

The answer demonstrates that you can change the default class template.

Hope that helps.


If you omit the modifier on a class it is either:

  • internal (non-nested classes)
  • private (on nested classes)

I just thought I'd throw that out there (that it's not ALWAYS internal if omitted).


But, if you really want to change templates you'll want to generally go to the installation folder (I'll use the default folders):

**C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE**

In there, you will find the various template folders. You're looking for the following (if you dig deeper): C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Just alter the Class.cs file in there. Oh, and in case you mess it up here's the original version:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

You're desired version:

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;

namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜