Questions tagged [using-directives]

The `using` directive, available in several languages including C# and C++, introduces members of a namespace into the current identifier search scope.

The C++ using directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. Of course, the complete, qualified name can still be used to improve readability.

// C++ Example:
using std::cout

The C# using directive is used to qualify namespaces and create namespace or type aliases. The scope of a using directive is limited to the file in which it appears.

// C# Example:
using System.Text;
using Project = PC.MyCompany.Project;

Creating an alias for a namespace or a type in C# is called a "using alias directive", and is illustrated in the code sample below:

namespace PC
{
    // Define an alias for the nested namespace. 
    using Project = PC.MyCompany.Project;
    class A
    {
        void M()
        {
            // Use the alias
            Project.MyClass mc = new Project.MyClass();
        }
    }
    namespace MyCompany
    {
        namespace Project
        {
            public class MyClass { }
        }
    }
}
289 questions
3326
votes
42 answers

Why is "using namespace std;" considered bad practice?

I have heard using namespace std; is bad practice, and that I should use std::cout and std::cin directly instead. Why is this? Does it risk declaring variables that share the same name as something in the std namespace?
akbiggs
  • 34,001
  • 6
  • 25
  • 36
525
votes
16 answers

The type or namespace name could not be found

I have a C# solution with several projects in Visual Studio 2010. One is a test project (I'll call it "PrjTest"), the other is a Windows Forms Application project (I'll call it "PrjForm"). There is also a third project referenced by PrjForm, which…
Anders
  • 15,227
  • 5
  • 32
  • 42
123
votes
7 answers

Visual Studio or Resharper functionality for placement of using directives

I like to put my using directives inside the current namespace, and not outside as VS and Resharper per default puts them. Does anyone know of a macro/standard functionality that sorts/removes unused using directives and puts them inside the current…
larsw
  • 3,790
  • 2
  • 25
  • 37
56
votes
2 answers

Why is "using System;" not considered bad practice?

I have a C++ background and I do fully understand and agree with the answers to this question: Why is “using namespace std;” considered bad practice? So I'm astonished that, having some experience with C# now, I see the exact opposite there: using…
sebrockm
  • 5,733
  • 2
  • 16
  • 39
53
votes
3 answers

Why doesn't a using-declaration work to solve the diamond problem?

Please consider the following code: struct A { void f() { } }; struct B1 : A { }; struct B2 : A { }; struct C : B1, B2 { void f() // works { B1::f(); } //using B1::f; // does not work //using B1::A::f; //…
gd1
  • 11,300
  • 7
  • 49
  • 88
40
votes
4 answers

C++: Should I use 'typedef' or 'using namespace'?

I am writing a library with mutiple dependent modules. When I include a file from a different module, should I resolve the namespace with: using namespace project1::namespace1; class1 obj; or typedef project1::namespace1::class1 class1; class1…
vidit
  • 957
  • 1
  • 8
  • 23
32
votes
1 answer

Why does C++'s "using namespace" work the way it does?

All students are surprised by the behavior of C++ using-directives. Consider this snippet (Godbolt): namespace NA { int foo(Zoo::Lion); } namespace NB { int foo(Zoo::Lion); namespace NC { namespace N1 { int…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
31
votes
1 answer

C++ 'typedef' vs. 'using ... = ...'

Possible Duplicate: What are the differences between typedef and using in C++11? The following code compiles and runs. My question is what is the difference between the "typedef" and "using" method for renaming the template specialization?…
Simon
  • 961
  • 2
  • 9
  • 14
29
votes
5 answers

Scoped using-directive within a struct/class declaration?

I find that my C++ header files are quite hard to read (and really tedious to type) with all the fully-qualified types (which goes as deep as 4 nested namespaces). This is the question (all the answers give messy alternatives to implementing it, but…
Zach Saw
  • 4,308
  • 3
  • 33
  • 49
28
votes
1 answer

Using std::array and using "array" as name

In my C++ JSON library, I recently had a regression with GCC7. I stripped down the affected code and hope to understand the error. The code Consider this header myclass.hpp: #pragma once template struct A { struct value_t { …
Niels Lohmann
  • 2,054
  • 1
  • 24
  • 49
27
votes
5 answers

The type or namespace cannot be found (are you missing a using directive or an assembly reference?)

I get the following error when I try to compile my C# program: The type or namespace name 'Login' could not be found (are you missing a using directive or an assembly reference?) using System; using System.Collections.Generic; using…
Leo
  • 465
  • 2
  • 9
  • 16
21
votes
3 answers

What does "using namespace" do exactly?

The following C++ test code does not link (gcc 4.9.2, binutils 2.25). The error is In function 'main': undefined reference to 'X::test'. 01: #include 02: #include 03: 04: namespace X 05: { 06: extern std::string test; 07:…
marcv81
  • 850
  • 8
  • 22
20
votes
3 answers

Default using directives in new C# files

Why does Visual Studio 2008 automatically insert the following using directives into each new C# file I create? using System; using System.Collections.Generic; using System.Text; What's so special about these namespaces? Are these the most…
compie
  • 10,135
  • 15
  • 54
  • 78
20
votes
1 answer

How can I detect unused imports in a Script (rather than a Document) with Roslyn?

I'm writing a system to process snippets written as unit tests for Noda Time, so I can include the snippets in the documentation. I've got a first pass working, but I wanted to tidy up the code. One of the things this needs to do when processing a…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
19
votes
3 answers

using directive vs using declaration swap in C++

Please refer to the code below: #include namespace N { template class C { public: void SwapWith(C & c) { using namespace std; // (1) //using std::swap; // (2) …
ilovekonoka
  • 303
  • 1
  • 2
  • 6
1
2 3
19 20