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.
Related
I have a C# 10 project with <ImplicitUsings> enabled:
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
With this in place, VS will gray-out many common namespaces in code files and offer to remove them.
However, when I create a new C# file it still imports all of the now-unnecessary using statements by default:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyNamespace
{
internal class Class1
{
}
}
Is this just a limitation of VS or is there something I can do to convince it to omit these namespaces from the new file template?
I think that the "ImplicitUsings" is not for what you look for. It only add some pre-defined usings to your project based on the project's SDK. See Implicit-using for reference.
But will not remove the usings directives of templates.
I'm having a problem with ReSharper refactoring I just can't find a solution to.
Whenever I'm trying to adjust a name space through refactoring, when it says "Move to 'correct.namespace' namespace", ReSharper forcefully removes unused "using" statements from all the files it fixes the namespace in and it also opens all the files.
My question is, how to prevent ReSharper from opening all the files it modifies and also how to stop it from removing unused usings?
I don't want it to open 20+ files, neither do I want it to remove the standard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Please help.
Add them to the Namespace Imports part of the Resharper Options to preserve the namespaces.
I'm not sure if its possible to prevent it from opening all of the files since it depends on so much of the VS infrastructure. Plus it would not be possible to undo without opening the files.
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
I'm working on app which uses a lot of external assemblies (newtonsoft.dll, Yahoo Yui compressor.dll, fleck.dll etc). In each c# file I need to add using statement with all those assemblies. Is it possible to create my own assembly (i.e. LIBRARY.dll) containing all the dll's and refer only to this in all c# files?
No. Firstly, using directives refer to namespaces, not assemblies (assembly references are defined at the project level). Secondly: you almost certainly don't actually need all of them in every file. But: you can create a new-file-template with the ones you are likely to need. But frankly it is usually easier to either copy/paste them, or just add them when they are needed. In the IDE, this is as simple as pressing ctrl+.,ret after a type name that doesn't resolve... so MySpecialTypectrl+.,ret should add the missing using directive to resolve MySpecialType.
using does not refer to assembly, but to namespace. So the answer is "no"...
using System; // you are using items in the System namespace
using System.IO; // you are using items in the System.IO namespace
No. Usages of individual types need to be resolved to the namespaces where they are defined in. So, you will still need to include the resolution paths in your usings.
I like to adhere to StyleCop's formatting rules to make code nice and clear, but I've recently had a problem with one of its warnings:
All using directives must be placed
inside of the namespace.
My problem is that I have using directives, an assembly reference (for mocking file deletion), and a namespace to juggle in one of my test classes:
using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: MoledType(typeof(System.IO.File))]
namespace MyNamespace
{
//Some Code
}
The above allows tests to be run fine - but StyleCop complains about the using directives not being inside the namespace.
Putting the usings inside the namespace gives the error that "MoledType" is not recognised.
Putting both the usings and the assembly reference inside the namespace gives the error
'assembly' is not a valid attribute
location for this declaration. Valid
attribute locations for this
declaration are 'type'. All attributes
in this block will be ignored.
It seems I've tried every layout I can but to no avail - either the solution won't build, the mocking won't work or StyleCop complains!
Does anyone know a way to set these out so that everything's happy? Or am I going to have to ignore the StyleCop warning in this case?
Solved two minutes later!
I just needed to put the full path of "MoledType" in the assembly reference - meaning I could leave it outside of the namespace with the using directives inside like so:
[assembly: Microsoft.Moles.Framework.MoledType(typeof(System.IO.File))]
namespace MyNamespace
{
using System;
using System.IO;
using Microsoft.Moles.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
// Some Code...
}
Hopefully someone'll find this useful!
The typical pattern would be to put all of your Assembly level attributes within the AssemblyInfo.cs file. Typically this file does not have any namespace element at all and all of the assembly attributes are defined using fully qualified names.
Agreed with Jason, you should put this in the AssemblyInfo.cs instead (Project -> Properties).
But! Be careful with what you put in the AssemblyInfo.cs file. Say you want to use:
[assembly: InternalsVisibleTo("ClassA")]
If you put this in AssemblyInfo.cs, ALL internal classes of the project will be visible to ClassA. This is not always wanted.