Questions tagged [using]

"using" is a keyword in some programming languages (C++, C#, VB.NET, Haxe)

In the C# language, the using keyword is employed in two different contexts, as a Directive and a Statement.

The using directive is used to qualify namespaces and create namespace or type aliases.

using System.Text;
using Project = PC.MyCompany.Project;

The using statement provides a convenient syntax that ensures the correct use of IDisposable objects.

using (MyTypeImplementingIDisposable myInstance)) 
{
    // Do something with myInstance
}

Beginning with C# 8.0, you can use the alternative syntax that doesn't require braces

using var myInstance = new MyTypeImplementingIDisposable(...);
// Do something with myInstance

In the VB.NET language the Using keyword is used only as a Statement and provides the same features as the C# language:

Using sr As New StreamReader(filename)
    ' read the sr stream
End Using

In Haxe, the using keyword allows pseudo-extending existing types without modifying their source (syntactical sugar). It is achieved by declaring a static method with a first argument of the extending type and then bringing the defining class into context through using.

using StringTools;

// works because of the `using`:
var myEncodedString = "Haxe is great".replace("great", "awesome");
// Without the using one should type this: 
var myEncodedString = StringTools.replace("Haxe is great", "great", "awesome");

In gnuplot, the using qualifier allows specific columns in a datafile to be specified, for plotting and fitting.


In C++, the using keyword can be used in 3 ways;

  1. using declarations

    using std::swap;

  2. using directives

    using namespace std;

  3. type alias and alias template declaration (since C++11); similar to typedef

    template <class CharT> using mystring = std::basic_string<CharT,std::char_traits<CharT>>;

1795 questions
34
votes
7 answers

Can I undo the effect of "using namespace" in C++?

With using namespace I make the whole contents of that namespace directly visible without using the namespace qualifier. This can cause problems if using namespace occurs in widely used headers - we can unintendedly make two namespaces with…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
34
votes
3 answers

Does "using" statement always dispose the object?

Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { //…
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
33
votes
2 answers

C++11: Why is private member template accessible outside class?

I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using directive: class wrapper { private: template class __tklass {}; class __klass {}; }; template…
nav
  • 1,645
  • 15
  • 22
29
votes
4 answers

Using various types in a 'using' statement (C#)

Since the C# using statement is just a syntactic sugar for try/finally{dispose}, why does it accept multiple objects only if they are of the same type? I don't get it since all they need to be is IDisposable. If all of them implement IDisposable it…
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
29
votes
16 answers

C# using statement catch error

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new…
PeteT
  • 18,754
  • 26
  • 95
  • 132
28
votes
10 answers

a way in c++ to hide a specific function

i have an inheritance struct A : public B, i want to hide individual functions from B, is this possible? i know the opposite is possible using using BMethod in the A declaration. cheers
lurscher
  • 25,930
  • 29
  • 122
  • 185
27
votes
6 answers

Getting rid of nested using(...) statements

Sometimes I need to use several disposable objects within a function. Most common case is having StreamReader and StreamWriter but sometimes it's even more than this. Nested using statements quickly add up and look ugly. To remedy this I've created…
Ghostrider
  • 7,545
  • 7
  • 30
  • 44
27
votes
5 answers

Any issue with nesting "using" statements in c#?

I recently downloaded Visual Studio 2013 and I ran the Code Analysis on a project I'm working on. Its thrown up a couple of issues that I'm working through but one in particular is about how I am using the "using" IDisposable statement. Here's an…
Rob
  • 6,819
  • 17
  • 71
  • 131
26
votes
5 answers

Curious C# using statement expansion

I've run ildasm to find that this: using(Simple simp = new Simple()) { Console.WriteLine("here"); } generates IL code that is equivalent to this: Simple simp = new Simple(); try { Console.WriteLine("here"); …
Matthew
  • 261
  • 3
  • 3
26
votes
7 answers

How to select a single overload of a function with using namespace::function in C++?

consider the following C++ code. namespace A { void f() { // first function } void f(int) { // second function } } ... using A::f; // introduces both functions Is there a way to introduce only one function?
MKo
  • 4,768
  • 8
  • 32
  • 35
26
votes
2 answers

Asynchronous methods in using statement

Note: I'm using C# in Unity, that means version .NET 3.5, so I cannot use await or async keyword.. What will happen to using statement when I put a method in it which works asynchronously? using (WebClient wc = new WebClient()) { …
Jenix
  • 2,996
  • 2
  • 29
  • 58
25
votes
6 answers

Unused using statements

I may already know the answer to this question, but I thought it was worth asking anyway. If I have a load of using statements within my code file that aren't being used; Does that have any sort of detrimental performance impact? How does the…
user448374
25
votes
6 answers

Entity Framework, DBContext and using() + async?

There is a thing that's been bugging me for a long time about Entity Framework. Last year I wrote a big application for a client using EF. And during the development everything worked great. We shipped the system in august. But after some weeks I…
Objective Coder
  • 557
  • 3
  • 11
  • 23
25
votes
9 answers

Catching Exception inside Using statement

I know that Using statement disposes out the object that is being created. Like if I wanted to do something like this: Using(SqlConnection conn = new SqlConnection(connString)) { //some code //How to show the users if conn is not…
RG-3
  • 6,088
  • 19
  • 69
  • 125
24
votes
3 answers

Using clause fails to call Dispose?

I'm using Visual Studio 2010 to target .NET 4.0 Client Profile. I have a C# class to detect when a given process starts/terminates. For this the class uses a ManagementEventWatcher, which is initialised as below; query, scope and watcher are class…
groverboy
  • 1,133
  • 8
  • 20