Class modifier for automatically generated classes - c#

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 way 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$
{
}
}

Related

How to use a "flat" namespace in whole C# project

I want to use the same namespace for my whole c# class library. But the default behavior for new files is to use a namespace like [Base namespace].[Folder 1].[Folder 2].
Is there a simple way or plugin for visual studio 2019 to make all new files use the same namespace? So just [Base namespace] whatever the folder?
But just for a single project, I don't want to change the global file templates.
Although all concerns mentioned in comments, there's simple way to achieve this. Just go to project properties and define default namespace:
Now every added class has this namespace defined:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyGlobalNamespae
{
class Class1
{
}
}
I have found a workaround I'm pleased with:
I installed this nice extension Visual Studio Namespace Fixer. It allows you to define a template for your namespaces and then can apply it to all .cs, .xaml, and other files in your project.
Under Options -> Namespace Fixer options -> Use default project namespace I defined the format {projectRootNamespace} for Namespace format.
Not I just have to select my files and folders in the projects base folder (in Visual Studio, not in the windows explorer), rightclick and select Adjust namespaces and all files in all subfolders will change their namespace to the projects root namespace :-)
Awesome if you create and use a lot of class and other files in your NuGet lib.

Visual Studio 2015 not inserting Using directives when generating class via Quick refactorings

This happens only when I generate a class using "Quick Actions and Refactorings...". For example, when I right click on the word Model and select "Quick Actions and Refactorings..." and then click "Generate class 'Model' in new file", this is what the created Model.cs file looks like:
namespace MyApp
{
internal class Model
{
}
}
I was expecting Visual Studio would automatically add the using directives like using System; using System.Collections.Generic; using System.Linq; using System.Text;. This doesn't happen if I added the class using Add->New Item.
I already checked the Tools->Options but to no avail. Appreciate any help. Thanks,
When add class through Quick Actions and Refactorings namespaces will not generate. Please see the documentation below.
https://learn.microsoft.com/en-us/visualstudio/csharp-ide/code-generation/generate-class-type

ASP.NET + Linq to SQL

I have a class in my App_Code directory that I'm trying to initialize from the root directory of my solution but it is not showing up in IntelliSense. I have tried changing the class's build action from Content to Compile but then IntelliSense throws a whole bunch of errors about not finding my DBML class.
Note DBML file, Class and aspx file are all in the same namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
^ Imports on class that I'm trying to initialize. (May be irrelevant)
Any pointers in the right direction would be appreciated.
You need to make sure that Build Action of none of the files in the App_Code folder is marked as Compile.
But this will bring its own side effects that intellisense may not
work very well for these files inside VS as they will not be treated
as Class files by VS… But the key point is that you do not really need
“App_Code in Web Application Projects (WAP) if you do not intend to
put random code files or modify existing code files in App_Code folder
directly on your production server…
http://vishaljoshi.blogspot.fi/2009/07/appcode-folder-doesnt-work-with-web.html

How to force a C# root namespace throughout project files for when none is coded?

I want to force a root namespace on the contents of any .cs source files that don't have their contents wrapped in an explicit namespace. In other words I want to keep classes and other namespace-level structures out of the default namespace.
(Working inside a Windows .NET environment with Visual Studio)
In the following example, I want to force the Car class into the MotorIndustry namespace by default even though it has no explicit namespace coded.
Vehicle.cs (has namespace)
namespace MotorIndustry {
public class Vehicle {
// ...
}
}
Car.cs (no namespace/default)
public class Car : Vehicle {
//...
}
Is there a way to accomplish this "root namespace" modification behaviour through project settings in Visual Studio, AssemblyInfo.cs file or some other means?
My understanding is VB.NET has a feature like this but C# acts differently?
Some context about why am I asking this: I have hundreds of classes and the programmer forgot to wrap the namespace around some. When I reference the assembly from other projects it's polluting the default namespace with classes that end up causing some ambiguous situations.
Use ReSharper to move the classes into the proper namespace. In fact, version 5.0 (still in Beta) allows you to correct namespaces globally.
The other way to do it is to make the other developer fix the code.
It sounds like you are trying to replicate VB.Net's project namespace feature in C#. This is not a supported feature of the C# compiler or IDE. You will not be able to create one by modifying a project file. You will need to add the namespace to every file in the project either manually or via a tool.

Class file in visual studio 2008

seWhen i creating a new class file i got these namespaces by default,
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
But i dont use linq,Html controls,Webcontrols,Configuration,security...
Why are they included by default?
What will happen if i exclude these from my class file?
What will happen if i include these without using them?
Why are they included by default?
Visual Studio adds a list of common includes to any class file that you create. Since it is an ASP.Net project, this is the list of using statement added to your file. For WinForms projects, it is a different set of usings.
What will happen if i exclude these from my class file?
If you don't use any classes within these namespaces within your file, excluding them will have no effect. If you use a class within those namespaces, you will have a compile error.
What will happen if i include them without using them?
Maybe it will take a few milliseconds longer to compile that file but I'm not even sure.
These namespaces are included by default in files that you add to an ASP.Net project.
The using statement simply tells the compiler which namespaces the classes you're using are in.
If you aren't using any classes from those namespaces, the using statement will no effect whatsoever; removing them or keeping them will not matter.
Caveat: If there are two classes with the same name in two different namespaces (eg, System.Windows.Forms.Control and System.Web.UI.Control), and you having using statements for both namespaces, you won't be able to use the class unless you fully qualify it with the namespace name. (Because the compiler cannot know which one you want)
Nothing will happen if you exclude them.
Nothing will really happen if you include them.
It's recommended to remove the ones you don't need.
Not sure about the Web stuff, but Linq is included by default for all 3.5 applications, probably because it's assumed that you'll use them as much as you do system.

Categories