The first statement of all my C# files is "using System;".
Now with framework version 4 this namespace contains a class called "Action". This is also the name for a class im my own code in a regularly used namespace. Now there is of course a conflict. Ofcourse I can resolve this conflict by using explicit "MyNamespace.Action" where ever I was using "Action" before. This are several hundreds if not thousands of lines. Or I could not use the System namespace, which of course leads to many other problems. I would like to write something like "using System but not System.Action", but I cannot find a statement to do this.
Any ideas?
No, you can't.
But you can add using Action = MyNamespace.Action. This will be highly confusing for new developers, though, as Action is a fundamental part of .net since 3.5 so I strongly suggest you rename your class.
The using directive has two uses:
To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:
using System.Text;
To create an alias for a namespace or a type (you could go for this one).
using Project = PC.MyCompany.Project;
The using keyword can also be used to create using statements, which define when an object will be disposed. See using statement for more information.
using directive (C# Reference)
Related
I have just looked in my regedit and found I am running .NET Version 4.5.1 and therefore the libraries I am after should be available to me.
I try to include the namespaces:
using System.IO.MemoryStream;
using System.Media.SoundPlayer;
However VS complains that the namespaces do not exist.
The reference pages for MemoryStream and SoundPlayer dictate that these should be available in my version of .NET. How can I fix this?
You are confusing class names and namespaces and possibly the using directive with the using statement.
System.IO is a namespace. It's a grouping construct used to logically group classes and structs together and avoid name clashes. System.IO.MemoryStream is a class inside the System.IO namespace.
If you want to use a MemoryStream you can either:
var ms = new System.IO.MemoryStream(...);
Or, to avoid the verbosity of always specifying the namespace (a full qualified name), you can use the using directive:
using System.IO;
and then you can just use the class name:
var ms = new MemoryStream(...);
You can think of the using directive as telling the compiler what search paths to use when looking for a class. If you ask for MemoryStream it's going to first look in the current namespace for a class named MemoryStream, if it doesn't find one, it's then going to look in all the namespaces that have been imported with the using directive (in VB.NET, I believe the equivalent directive is actually called Imports which, arguably, makes more sense, but there you go). If it still doesn't find a MemoryStream class you'll get a compile time error.
Note: you'll also get a compile time error if it finds more than one MemoryStream because it won't stop on the first one - your classes need to be unambiguous. And this is a reason not to just stuff a lot of unneeded using directives at the top of every .cs file. MemoryStream isn't particularly a problem here (I think it's the only MemoryStream in the BCL), but another class in System.IO is Path. There are several classes called Path in the BCL (There is another one under System.Windows.Shapes) and without namespaces they'd be a nightmare to use.
Now, your confusion might come from the using statement which is often used with classes what implement IDisposable to ensure they get disposed. MemoryStream implement IDisposable so you'll often see things like:
using (var ms = new MemoryStream(...))
{
// some code here
}
Or, if they haven't used the using directive to declare the namespace:
using (var ms = new System.IO.MemoryStream(...))
{
// some code here.
}
Which looks a lot like what you were trying to do with your using directive.
When looking at a class in MSDN there are two important things you need to look for:
Namespace: System.IO
Assemblies: mscorlib (in mscorlib.dll)
System.IO (in System.IO.dll)
Namespace tells you what you need to include either in a using directive or as part of a fully qualified class name in order to use the class. The Assemblies part tells you which assemblies you need to add a references to your projects in order to be able to use those classes. In this case MemoryStream is part of the core libraries, so you aren't likely to not have a reference to the required assemblies.
Just use the namespace only
using System.IO;
using System.Media;
Using System is the right thing to do as MemoryStream is class in itself.
Right Click Project -->Add reference --> Assemblies -->System
Using System.IO
Then in code you can make object of MemoryStream.
I thought I'd add one more point, regarding the format of your using directive. You can use a using directive very nearly like that, in order to create an alias for a namespace or class.
For instance, creating an alias for a class like this:
using SP = System.Media.SoundPlayer;
Allows you to do this:
var sp = new SP();
This is such a dumb question, but I can't figure out the lingo to ask Google.
In Java if I wanted to import all subclasses I would use something like
java.util.*
And all of util would be imported.
Firstly, what is the proper lingo for what I'm doing in C# so I can start using Google more effectively. Am I importing namespaces? Libraries? Subclasses? (Can you tell I'm new at this?)
Secondly, since I'm here, how is this accomplished in C#?
PS- I did click on every related question stackOverflow threw at me to see if the answer would pop up. No luck. I'm simply without words to describe what I'm looking for. The example should do just fine but... Anyone who can take a moment to either explain the lingo to me or perhaps simply point me at something that can (in a nutshell, I have a couple books for the long haul) that would be great.
Firstly, let's differentiate between assembly references and namespaces.
Assemblies are what you add references to in your c# project, they are the libraries that contain the actual classes you need, usually found as DLL files. The .net framework contains many such assemblies, and Visual Studio will try to reference the most commonly used ones in your project (e.g. for a WinForms project it will automatically add a reference to System.Drawing.dll).
Namespaces are logical partitions of the classes in an assembly.
Once you reference an assembly in the project, all classes in all namespaces are available for use, if you provide their full name.
This is where the using directive comes in.
It is simply syntactic sugar for not having to write very long names all the time.
For example, assuming your project references the System.Drawing.dll assembly, you would have to qualify a class from this assembly using it's full name, for example
System.Drawing.Imaging.BitmapData
Because this is tiresome and bloats the code, if you start your .cs file with
using System.Drawing.Imaging;
you could then instantiate a class using just the name BitmapData.
This will be true only for the .cs file in which you added the using directive, not for the whole project.
Also, it's important to note that using one namespace does not bring in all nested namespaces, you have to using each one individually.
You must brush up on your your Google-fu
http://www.harding.edu/fmccown/java_csharp_comparison.html#namespaces
It can be called importing / referencing/ using namespace.
Such a language feature is not available in c#.
A little explanation: Namesspaces can be spread across multiple libraries. so when you use a namespace it may refer to it from multiple referenced assemblies.
It's called namespace and it's imported by a keyword using. For example:
using System;
This statement enables you to reference all the classes that exist in that namespace. They, however don't enable you to reference any class in the subnamespace of declared namespace. You have to declare each namespace separately. For example:
using System;
using System.Text;
Of course, you need to have a proper references added to the project where you're specifying the using directive.
Within .Net, you first would need to ensure that there is a referenced assembly containing the namespace you would like to import. Once that reference exists, you can use the 'using' directive to bring that namespace into the class so as to not have to fully qualify all object names. You can find more information on this directive on MSDN.
If I misunderstood, let me know and I'll do my best to get you pointed in the right direction.
If my base class has something like:
using System.Web.Mvc;
using System.Web.Routing;
using MFA.Storage.Helpers;
using MFA.Storage.Models;
using MFA.WebUx.Areas.Administration.ViewModels.Notes;
using MFA.WebUx.Areas.Administration.Services.Notes;
using MFA.WebUx.Areas.Administration.ActionFilters
using System;
Then in my child classes that inherit from the base controller do I still need to add all the using directives? Is that a good or bad practice?
using directives do not "inherit" (their scope is confined to the file in which they are declared), so you will need to add them to the descendant class as well, unless both classes reside in the same file.
From a best practice perspective, it's probably better to keep each class in it's own file, and add using directives to each class file as needed.
You definitely don't NEED those using directives if you don't use them. The only one you need for sure is the namespace of the base controller.
However, it's not really considered "bad practice" to include them ... many of Visual Studio's default files have several usings that aren't used by default. As long as the extra usings don't create conflicts, then just keep them there. It's much easier to leave them there than it is to add them back later.
As a side note, there are some GREAT plugins that help you organize the using directives. I use Resharper, which "grays-out" any useless using directives, indicating that they can be removed. And when a using directive is missing, it finds it and lets me add it easily.
Visual Studio 2010 also lets you add missing using directives, so it's not a big deal to remove extra usings.
I'm pretty sure I know the answer but I'm wondering if there's a way to define a global "using" directive in my C# projects so that I don't have to repeat the directive on top of every code file.
My interest is really rooted with the introduction of extension methods in the .NET Framework. The only way to use an extension method is to define a using directive for the namespace containing the extension methods. Without the using directive, I lose Intellisense capabilities for the extension methods which means I won't always see what methods are available.
As a framework developer, making sure that the types and methods that are provided in the framework are clear and available to consuming developers is key to me. While documentation and training serve their purpose, I've found that most devs will hit the period and scroll through the Intellisense list to see what methods and properties are available. Even if they go to the Object Browser or view the reference documentation, they won't know about an extension method unless they know about it. This is where Intellisense comes in.
And, while I can add the using directive to the template VS uses, the "Remove and Sort" option in VS will remove the directive referencing the extension methods if one isn't being used.
So, all that being said, is there any way to define a global "using" directive in VS 2010? If not, any chance it's being considered by MS for the future?
You can place your extension methods in the global namespace, by making sure the containing classes are not declared within a namespace. This will make them globally available.
As for default namespaces - apart from changing the VS templates, this is not possible.
Of course, it is always possible the MS will consider it. Unlikely, but possible.
Instead of looking for a technical solution, perhaps you should look at educating your fellow developers and provide a convention regarding extension methods (for instance, a known location/namespace schema)?
The global using directive is a new language feature in C# 10. When running on .NET 6 there are also implicit global using directives available in certain namespaces.
In VS 2010 there is no option to do it but in VS 2022 with C# 10.0 you can use Global Usings.
global using <TheNamespace>;
To answer your actual problem, if you put your static class with extensions in no namespace, it will be available everywhere. Use with care!
If it's really important to you, you can have your company buy ReSharper and install it on all development machines, having this will give you Intellisense for those methods, with option to "auto add" the using directive.
From what I heard (didn't use it myself) the ReSharper scan all possible assemblies and I assume you can configure it to add "using" directive automatically when creating new class, though I'm not 100% sure in this.
You can create .net file templates for classes that contain your using directives.
See Creating Project and Item Templates (https://msdn.microsoft.com/en-us/library/ms247121.aspx).
You can save the template as part of the project you have your extensions in; however, this is best done in a different project.
Global using directives are introduced within C# 10.
It can be used through the same way as local using directives, except that you place a global keyword in front of it, in order to instruct the compiler to give access to the following directive accross the entire project:
global using System;
The recommended standard is to put all of your global using directives in a Usings.cs file to separate them from the rest of the project.
On MSDN I can read what it does, but I would like to know what it does technically (tells compiler where to look for types..)? I mean using as a directive.
The primary function of the using directive is to make types within a namespace available without qualification to the user code. It considers the set of namespaces and types which are defined in referenced assemblies and the project being compiled.
Take for example the following definition in MyTypes.Dll
namespace MyTypes {
class Class1 {}
}
Now consider referencing MyTypes.dll from another project with a different namespace. Without a using directive to create Class1 i need to qualify the name
MyTypes.Class1 local1 = new MyTypes.Class1();
The using directive lets me remove this qualification
using MyTypes;
...
Class1 local1 = new Class1();
#JaredPar's answer is correct, however I'd like to add that it doesn't quite work the same way as say import in Java. (someone correct me if I'm wrong about Java's import actually importing it into memory)
You need to include either a DLL or project reference in order to even be able to use using however it's not loaded into memory until you actually call into a method/property/something in the assembly. So you could have using System.Linq; but if you don't actually use any Linq methods, the Linq assembly is never loaded. (I'm not 100% positive that Linq is in it's own physical assembly since namespaces and assemblies aren't 1:1, but for sake of example I'm assuming it is)
using informs the compiler which namespaces to search for names used in the file but not defined in the file.
using just tell the compiler to go to the assembly's configuration file and search for a specific DLL with the name given, if the DLL is found then it links that dll into the current project. using is just a link operation to have DLL talk to each other in a shared space in memory.
The guy below here is right