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
12
votes
1 answer

What's the meaning of the highlighted sentence below in [over.load]/1?

What is the meaning of the highlighted sentence below? Does it have anything to do with function templates? [over.load]/1: Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is…
Alexander
  • 2,581
  • 11
  • 17
12
votes
1 answer

How to prevent ReSharper from shortening namespaces when adding using directives?

When I use ReSharper to add a using directive (using Alt+Enter) it removes "unnecessary" parts of the namespace. I prefer using the full namespace which is also the behavior of Visual Studio. Example: namespace MyCompany.MyTool.Data { // This…
Albic
  • 3,559
  • 4
  • 22
  • 25
11
votes
6 answers

Ordering of using namespace std; and includes?

I recently saw this code being used in a source file in a C++ project: using namespace std; #include Ignoring all issues of whether it's a good idea to have using namespace std at all, is the above code even legal? There is no code in…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
11
votes
2 answers

Why does public overload conflict with private using directive on some compilers?

I came across the following situation in one of my projects, where a base class has a function template, which is hidden by a non-templated function in a derived class template. Further down the class hierarchy, the non-templated function is…
pah
  • 313
  • 1
  • 10
11
votes
1 answer

What is the namespace 'Standard'?

When I try to write a new using clause, I notice that Intellisense has, in its list, a namespace called Standard. However, this seems to have no members on closer inspection. What is this namespace?
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
10
votes
6 answers

Why is std:: used by experienced coders rather than using namespace std;?

Possible Duplicate: Why is 'using namespace std;' considered a bad practice in C++? The other day when I asked a question someone replied saying if someone asks a question, show them the right way to do it instead of using namespace std; which I…
FuzionSki
  • 451
  • 1
  • 6
  • 8
10
votes
2 answers

Twitterizer 2 and C# - Namespace could not be found

I have a silly problem with Twitterizer2 and probably me :) . I add the reference twitterizer 2.3.1 from my downloads directory along with the newtonsoft one by right clicking on references and browsing to find them. I then add using…
9
votes
1 answer

Should a using command issue a warning when using a reserved identifier?

When using the line using std::literals::chrono_literals::operator""s; in g++ 6.3.0, the compiler issues a warning stating: warning: literal operator suffixes not preceded by '_' are reserved for future standardization using…
9
votes
3 answers

Namespace references in C# vs. VB.Net

In VB.Net you can do something like the following without any issues... just ignore the fact that this is a pretty useless class :-) Imports System Public Class Class1 Public Shared Function ArrayToList(ByVal _array() As String) As…
tcnolan
  • 498
  • 6
  • 12
9
votes
2 answers

Angularjs - Dynamically change dom with directives or widgets?

my goal is to understand how to use angularJS correctly. I want to be able to tie a selection of variable to dynamically changing the DOM structure using angularJS. I dont think I'm quite understanding the documentation that angular provides and I…
kman
  • 230
  • 1
  • 3
  • 8
8
votes
4 answers

Automatically call visual studio 2008 "sort using directives" on save?

Visual Studio 2008 got two great features for c#, which is called "sort using directives" and "remove unused using directives". I'd like to call the "sort using directives" every time I format the code using ctrl+k,ctrl+d. Or, even better, I would…
Sam
  • 28,421
  • 49
  • 167
  • 247
8
votes
2 answers

"Using" declaration with scope only on current class?

Is there a possibility to have a using directive whose scope is limited to a single class? Note, that what I want to "use" is not contained in the parent of the current class. For simplicity, assume the following exmple: #include class…
S.H
  • 875
  • 2
  • 11
  • 27
7
votes
2 answers

Why can't I write IO.Directory.GetFiles?

I come from a VB.Net environment, where using Imports System and then IO.Directory.GetFiles(...) works. On the other hand, it seems that using System; is not sufficient to write use IO.Directory without prefixing it with System.. The only…
Clément
  • 12,299
  • 15
  • 75
  • 115
7
votes
2 answers

GCC error when using parent class method as derived class method

I have a function in my code which only accepts a class member method as a template parameter. I need to call this method using a class method which is inherited from a parent class. Here is an example code of my problem: template class…
Toboxos
  • 170
  • 9
7
votes
5 answers

Ways to make use of 'using' directives in C# less tedious

Good programming practice these days tends to mean splitting your stuff up into lots of assemblies and namespaces (for example, see S#arp Architecture, MVC, etc.). However a side-effect of that is that you have to stick a whole bunch of 'using '…
codeulike
  • 22,514
  • 29
  • 120
  • 167
1 2
3
19 20