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
423
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
  • 11,594
  • 5
  • 43
  • 53
412
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
  • 21,803
  • 13
  • 83
  • 116
362
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
  • 7,378
  • 5
  • 28
  • 36
356
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,587
  • 3
  • 17
  • 5
351
votes
6 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
  • 6,893
  • 5
  • 33
  • 54
324
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
  • 8,284
  • 5
  • 33
  • 47
231
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,967
  • 10
  • 35
  • 41
216
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,485
  • 5
  • 30
  • 55
200
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
  • 55,237
  • 33
  • 144
  • 193
168
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
  • 70,800
  • 18
  • 132
  • 147
150
votes
8 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,407
  • 3
  • 31
  • 54
137
votes
2 answers

C# 8 understanding await using syntax

I have next method: public async Task> GetQuotesAsync() { using var connection = new SqlConnection(_connectionString); var allQuotes = await connection.QueryAsync(@"SELECT [Symbol], [Bid], [Ask], [Digits] FROM…
Uriil
  • 11,948
  • 11
  • 47
  • 68
135
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
  • 14,299
  • 46
  • 120
  • 156
128
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,886
  • 4
  • 28
  • 33
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
  • 189,012
  • 314
  • 712
  • 1,047
1
2 3
99 100