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
50
votes
2 answers

Do you need to call Flush() on a stream or writer if you are using the “using” statement?

I am not sure whether I need to call Flush() on the used objects if I write something like this: using (FileStream...) using (CryptoStream...) using (BinaryWriter...) { // do something } Are they always automatically flushed? When does the…
Ben
  • 2,435
  • 6
  • 43
  • 57
50
votes
4 answers

Will a using block close a database connection?

using (DbConnection conn = new DbConnection()) { // do stuff with database } Will the using block call conn.Close()?
Craig Johnston
  • 7,467
  • 16
  • 40
  • 47
50
votes
11 answers

C# "Using" Syntax

Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff } If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it?
Gordon Thompson
  • 4,764
  • 8
  • 48
  • 62
49
votes
4 answers

overhead to unused "using" declarations?

I've just installed resharper and it's letting me know the namespaces i'm not actually using in each of my classes. which lead me to the question - is there actually any overhead in leaving these, unused, using declarations in? is it just a matter…
nailitdown
  • 7,868
  • 11
  • 36
  • 37
45
votes
2 answers

MemoryStream in Using Statement - Do I need to call close()

When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here? using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); }
AJM
  • 32,054
  • 48
  • 155
  • 243
43
votes
3 answers

what does `using std::swap` inside the body of a class method implementation mean?

I was trying to learn and adopt the copy-swap idiom following this thorough explanation on this question: the Copy-Swap Idiom. But I found some code I had never seen: using std::swap; // allow ADL in this example class dumb_array { public: //…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
43
votes
4 answers

How to return a Stream from a method, knowing it should be disposed?

I have a method that takes FileStream as input. This method is running inside a for loop. private void UploadFile(FileStream fileStream) { var stream = GetFileStream(); // do things with stream } I have another method which creates and…
Frank Martin
  • 3,147
  • 16
  • 52
  • 73
43
votes
10 answers

Is it possible to force the use of "using" for disposable classes?

I need to force the use of "using" to dispose a new instance of a class. public class MyClass : IDisposable { ... } using(MyClass obj = new MyClass()) // Force to use "using" { }
Zanoni
  • 30,028
  • 13
  • 53
  • 73
42
votes
6 answers

Combining foreach and using

I'm iterating over a ManageObjectCollection.( which is part of WMI interface). However the important thing is, the following line of code. : foreach (ManagementObject result in results) { //code here } The point is that ManageObject also…
apoorv020
  • 5,420
  • 11
  • 40
  • 63
42
votes
8 answers

Manually destroy C# objects

I am fairly new to learning C# (from Java & C++ background) and I have a question about manual garbage disposal: is it even possible to manually destroy an object in C#? I know about the IDisposable interface, but suppose I'm dealing with a class…
East of Nowhere
  • 1,354
  • 4
  • 18
  • 27
41
votes
7 answers

using statement FileStream and / or StreamReader - Visual Studio 2012 Warnings

The new Visual Studio 2012 is complaining about a common code combination I have always used. I know it seems like overkill but I have done the following in my code 'just to be sure'. using (var fs = new FileStream(filePath, FileMode.Open,…
JHubbard80
  • 2,217
  • 1
  • 20
  • 24
39
votes
2 answers

Why do type aliases in C++ use 'using' instead of 'typedef' in their syntax?

Clearly, type aliases and templated type aliases are semantically equivalent to typedefs and an extension of typedefs to support template. How come new syntax with the using keyword was created for these instead of using typedefs for the first and…
tohava
  • 5,344
  • 1
  • 25
  • 47
35
votes
4 answers

Difference in using namespace (std:: vs ::std::)

using ::std::...; VS using std::...; Is there a difference(s)? If so, which one(s)? I saw this: using ::std::nullptr_t; which made me wonder.
gsamaras
  • 71,951
  • 46
  • 188
  • 305
35
votes
9 answers

"using" function

I've defined 'using' function as following: def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A = try { f(closeable) } finally { closeable.close() } I can use it like that: using(new PrintWriter("sample.txt")){ out => …
Albert Cenkier
  • 351
  • 1
  • 3
  • 4
34
votes
0 answers

Inequivalence of templated types using aliased template names

My goal is to record the name of a templated type when defining other types with template template parameters. The problem I have encountered is that the resulting types are considered to be inequivalent. To achieve this I have tried using a…
AngelGabriel
  • 694
  • 4
  • 12