Questions tagged [using-statement]

A `using` statement is a C# and VB.NET language feature that simplifies deterministic cleanup of disposable resources. Not to be confused with the (C# only) `using` directive (related to namespaces), for which use tag `using-directives`.

It takes an expression that evaluates to an IDisposable. After that it executes an associated code block. Once the code block exits(both with normal and exceptional exits) it disposes the result of the original expression.

C# Example:

using (StreamReader sr = new StreamReader("c:\file.txt"))
{
    //statements
}

VB.NET Example:

Using sr As New StreamReader("c:\file.txt")
    'statements
End Using

For Details check:

using Statement (C# Reference)

Using Statement (Visual Basic Reference)

446 questions
12
votes
5 answers

Can I combine a using() {} block with a method's out parameter?

Given a method public static bool Connection.TryCreate(out Connection connection) {} And a piece of calling code: Connection connection; if (!Connection.TryCreate(out connection)) // handle failure gracefully. /* * work with connection * *…
Sören Kuklau
  • 19,454
  • 7
  • 52
  • 86
12
votes
1 answer

NullReferenceException when creating ObjectContext in Using statement

Time once again to appeal to greater minds. I'm experiencing a very strange phenomenon. As the title states, I'm getting a NullReferenceException when trying to create an EF ObjectContext, but I only get the exception if I create the context within…
12
votes
2 answers

HttpClient in using statement causes Task cancelled

I created a FileResult : IHttpActionResult webapi return type for my api calls. The FileResult downloads a file from another url and then returns the stream to the client. Initially my code had a using statement like below: public async…
Rafi
  • 2,433
  • 1
  • 25
  • 33
12
votes
2 answers

C#, weird optimization

I'm trying to read my compiled C# code. this is my code: using(OleDbCommand insertCommand = new OleDbCommand("...", connection)) { // do super stuff } But! We all know that a using gets translated to this: { OleDbCommand insertCommand =…
Anemoia
  • 7,928
  • 7
  • 46
  • 71
12
votes
5 answers

Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my…
David Mason
  • 2,917
  • 4
  • 30
  • 45
11
votes
2 answers

Using Reflection.Emit to emit a "using (x) { ... }" block?

I'm trying to use Reflection.Emit in C# to emit a using (x) { ... } block. At the point I am in code, I need to take the current top of the stack, which is an object that implements IDisposable, store this away in a local variable, implement a using…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
11
votes
7 answers

return the variable used for using inside the using C#

I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; …
di3go
  • 133
  • 1
  • 1
  • 5
11
votes
4 answers

.NET using block and return; keyword

When I say this using (Entities db = new Entities()) { return db.TableName.AsQueryable().ToList(); } Do I by-pass the functionality of using block since I return something, and the method exits before exiting the using block, so I think the…
Snoop Dogg
  • 335
  • 1
  • 4
  • 13
11
votes
2 answers

Using for IDbConnection/IDbTransaction safe to use?

While my assumption may seem to sound subjective, after some research, I found that it's not uncommon to find developers who favour a dummy Try/Catch instead of using the Using statement for IDbConnection/IDbTransaction processing…
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
11
votes
4 answers

Exception of type 'System.OutOfMemoryException' was thrown. C# when using IDataReader

I have an application in which I have to get a large amount of data from DB. Since it failed to get all of those rows (it's close to 2,000,000 rows...), I cut it in breaks, and I run each time the sql query and get only 200,000 rows each time. I use…
DA_Prog
  • 263
  • 3
  • 7
  • 14
10
votes
4 answers

using & try/catch nesting

This question is more of a what is the RIGHT way to do something... The question...is there a proper nesting order between a using block and a try/catch? Is it ok to nest the entire using statement inside of a try/catch and maintain the benefits of…
Jared
  • 5,840
  • 5
  • 49
  • 83
10
votes
3 answers

Problems with the Using Statement and WCF client

I've been wrapping all the code that invoke WCF calls within an using statement in a thought that the object will be disposed properly. When I'm googling for an exception "Http service located at .. is too busy" I found this link…
VJAI
  • 32,167
  • 23
  • 102
  • 164
10
votes
1 answer

How to stop ReSharper from removing unused Using statements when moving/updating Namespace declarations?

When using ReSharper to move/update namespace declarations, is there a way to stop ReSharper from removing unused Using statements? In other words, if I have a class such as: using System.Collections.Generic; using System.Linq; using…
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
10
votes
1 answer

What is the standard conform syntax for template constructor inheritance?

GCC 4.8.1 accepts template class Subclass : public Baseclass { public: using typename Baseclass::Baseclass; }; but MSVC does not. On the other hand, MSVC accepts template class Subclass : public…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
10
votes
7 answers

Are all disposable objects instantiated within a using block disposed?

This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO…
Sky Sanders
  • 36,396
  • 8
  • 69
  • 90