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>>;

1664 questions
413
votes
26 answers

What is the best workaround for the WCF client `using` block issue?

I like instantiating my WCF service clients within a using block as it's pretty much the standard way to use resources that implement IDisposable: using (var client = new SomeWCFServiceClient()) { //Do something with the client } But, as…
Eric King
  • 10,848
  • 5
  • 39
  • 49
364
votes
12 answers

Do HttpClient and HttpClientHandler have to be disposed between requests?

System.Net.Http.HttpClient and System.Net.Http.HttpClientHandler in .NET Framework 4.5 implement IDisposable (via System.Net.Http.HttpMessageInvoker). The using statement documentation says: As a rule, when you use an IDisposable object, you should…
Fernando Correia
  • 20,349
  • 10
  • 79
  • 113
345
votes
17 answers

Nested using statements in C#

I am working on a project. I have to compare the contents of two files and see if they match each other precisely. Before a lot of error-checking and validation, my first draft is: DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory…
SBurris
  • 6,988
  • 5
  • 26
  • 34
336
votes
29 answers

What are the uses of "using" in C#?

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of using?
ubermonkey
  • 3,387
  • 3
  • 15
  • 5
289
votes
9 answers

What is the C# Using block and why should I use it?

What is the purpose of the Using block in C#? How is it different from a local variable?
Ryan Michela
  • 7,744
  • 5
  • 31
  • 47
283
votes
5 answers

MySQL JOIN ON vs USING?

In a MySQL JOIN, what is the difference between ON and USING()? As far as I can tell, USING() is just more convenient syntax, whereas ON allows a little more flexibility when the column names are not identical. However, that difference is so minor,…
Nathanael
  • 5,925
  • 4
  • 27
  • 45
226
votes
14 answers

Why should you remove unnecessary C# using directives?

For example, I rarely need: using System.Text; but it's always there by default. I assume the application will use more memory if your code contains unnecessary using directives. But is there anything else I should be aware of? Also, does it make…
steffenj
  • 7,729
  • 10
  • 33
  • 41
204
votes
12 answers

Should I Dispose() DataSet and DataTable?

DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods. However, from what I've read so far, DataSet and DataTable don't actually have any unmanaged resources, so Dispose() doesn't…
mbeckish
  • 10,108
  • 5
  • 27
  • 47
185
votes
5 answers

Will Dispose() be called in a using statement with a null object?

Is it safe to use the using statement on a (potentially) null object? Consider the following example: class Test { IDisposable GetObject(string name) { // returns null if not found } void DoSomething() { using…
Paolo Tedesco
  • 49,782
  • 29
  • 130
  • 181
142
votes
7 answers

in a "using" block is a SqlConnection closed on return or exception?

First question: Say I have using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string storedProc = "GetData"; SqlCommand command = new SqlCommand(storedProc, connection); command.CommandType…
Marcus
  • 5,166
  • 3
  • 28
  • 51
131
votes
2 answers

How do I use the C#6 "Using static" feature?

I'm having a look at a couple of the new features in C# 6, specifically, "using static". using static is a new kind of using clause that lets you import static members of types directly into scope. (Bottom of the blog post) The idea is as…
Cerbrus
  • 60,471
  • 15
  • 115
  • 132
126
votes
5 answers

Are there any side effects of returning from inside a using() statement?

Returning a method value from inside a using statement that gets a DataContext seems to always work fine, like this: public static Transaction GetMostRecentTransaction(int singleId) { using (var db = new DataClasses1DataContext()) { …
Edward Tanguay
  • 176,854
  • 291
  • 683
  • 1,015
121
votes
10 answers

Why remove unused using directives in C#?

I'm wondering if there are any reasons (apart from tidying up source code) why developers use the "Remove Unused Usings" feature in Visual Studio 2008?
bounav
  • 4,220
  • 4
  • 24
  • 28
119
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 Bubriski
  • 18,881
  • 34
  • 115
  • 167
116
votes
6 answers

How is performance affected by an unused using directive?

Visual Studio will automatically create using statements for you whenever you create a new page or project. Some of these you will never use. Visual Studio has the useful feature to "remove unused usings". I wonder if there is any negative effect on…
KdgDev
  • 13,321
  • 45
  • 113
  • 147
1
2 3
99 100