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
124
votes
8 answers

Do I have to Close() a SQLConnection before it gets disposed?

Per my other question here about Disposable objects, should we call Close() before the end of a using block? using (SqlConnection connection = new SqlConnection()) using (SqlCommand command = new SqlCommand()) { command.CommandText = "INSERT…
John B
  • 20,062
  • 35
  • 120
  • 170
116
votes
6 answers

Is it a good approach to call return inside using {} statement?

I just want to know is it safe/ good approach to call return inside a using block. For ex. using(var scope = new TransactionScope()) { // my core logic return true; // if condition met else return false; scope.Complete(); } We know the at…
user240141
109
votes
6 answers

Why is "using namespace X;" not allowed at class/struct level?

class C { using namespace std; // error }; namespace N { using namespace std; // ok } int main () { using namespace std; // ok } I want to know the motivation behind it.
iammilind
  • 68,093
  • 33
  • 169
  • 336
107
votes
5 answers

Can "using" with more than one resource cause a resource leak?

C# lets me do the following (example from MSDN): using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f)) { // Use font3 and font4. } What happens if font4 = new Font throws? From what I understand font3 will…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
80
votes
5 answers

Why should I use the "using" keyword to access my base class method?

I wrote the below code in order to explain my issue. If I comment the line 11 (with the keyword "using"), the compiler does not compile the file and displays this error: invalid conversion from 'char' to 'const char*'. It seems to not see the method…
Julien Vaslet
  • 1,804
  • 1
  • 14
  • 17
75
votes
14 answers

When should I use "using" blocks in C#?

Are there particular instances where I should (or shouldn't?) be using "using" blocks: using(SomeType t = new SomeType()){ ... }
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
66
votes
8 answers

Is there a situation in which Dispose won't be called for a 'using' block?

This was a telephone interview question I had: Is there a time when Dispose will not be called on an object whose scope is declared by a using block? My answer was no - even if an exception happens during the using block, Dispose will still be…
AdamCrawford
  • 4,968
  • 1
  • 18
  • 12
65
votes
8 answers

Does Stream.Dispose always call Stream.Close (and Stream.Flush)

If I have the following situation: StreamWriter MySW = null; try { Stream MyStream = new FileStream("asdf.txt"); MySW = new StreamWriter(MyStream); MySW.Write("blah"); } finally { if (MySW != null) { MySW.Flush(); …
JasonRShaver
  • 4,344
  • 3
  • 32
  • 39
65
votes
4 answers

If an Exception happens within a using statement does the object still get disposed?

If an Exception happens within a using statement does the object still get disposed? The reason why I'm asking is because I'm trying to decide on whether to put a try caught around the whole code block or within the inner using statement. Bearing in…
Andrew
  • 9,967
  • 10
  • 64
  • 103
62
votes
2 answers

What is the use of "using namespace std"?

What is the use of using namespace std? I'd like to see explanation in Layman terms.
Jarvis
  • 629
  • 1
  • 6
  • 4
61
votes
9 answers

Using statement vs. IDisposable.Dispose()

It has been my understanding that the using statement in .NET calls an IDisposable object's Dispose() method once the code exits the block. Does the using statement do anything else? If not, it would seem that the following two code samples achieve…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
60
votes
0 answers

"using" keyword in java

In Java is there an equivalent to the C# "using" statement allowing to define a scope for an object: using (AwesomeClass hooray = new AwesomeClass()) { // Great code }
Jla
  • 11,304
  • 14
  • 61
  • 84
59
votes
5 answers

scope of using declaration within a namespace

Is it safe (and correct) in a C++ header file to use the using declaration within a namespace as follows: #include namespace MyNamespace { using boost::numeric::ublas::vector; vector MyFunc(vector…
Brett Ryland
  • 1,045
  • 1
  • 9
  • 17
55
votes
2 answers

yield return statement inside a using() { } block Disposes before executing

I've written my own custom data layer to persist to a specific file and I've abstracted it with a custom DataContext pattern. This is all based on the .NET 2.0 Framework (given constraints for the target server), so even though some of it might look…
Neil Fenwick
  • 6,106
  • 3
  • 31
  • 38
52
votes
6 answers

Using Statement with Generics: using ISet<> = System.Collections.Generic.ISet<>

Since I am using two different generic collection namespaces (System.Collections.Generic and Iesi.Collections.Generic), I have conflicts. In other parts of the project, I am using both the nunit and mstest framework, but qualify that when I call…
Jason More
  • 6,983
  • 6
  • 43
  • 52