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
29
votes
16 answers

C# using statement catch error

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new…
PeteT
  • 18,754
  • 26
  • 95
  • 132
28
votes
4 answers

Are resources disposed even if an exception is thrown in a using block?

Possible Duplicate: Does Dispose method still get called when Exception is thrown inside of Using statment? I've got a number of using blocks, when accessing a database. I was wondering - if an exception had to be thrown within the using block,…
Dot NET
  • 4,891
  • 13
  • 55
  • 98
24
votes
5 answers

Sort "usings" directive by ReSharper Clean-up

How to setup ReSharper to call "Sort usings" in class?
szkra
  • 1,423
  • 2
  • 14
  • 21
24
votes
5 answers

What is the Managed C++ equivalent to the C# using statement

How would one code the following C# code in Managed C++ void Foo() { using (SqlConnection con = new SqlConnection("connectionStringGoesHere")) { //do stuff } } Clarificaton: For managed objects.
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
22
votes
6 answers

Using Statements vs Namespace path? C#

I recently stopped using using-statements and instead use the full namespace path of any .net object that I call. Example: using System; namespace QuizViewer { class Class1 { Console.WriteLine("Hello World!"); } } This is…
Kyle Uithoven
  • 2,414
  • 5
  • 30
  • 43
22
votes
3 answers

C#: "Using" Statements with HttpWebRequests/HttpWebResponses

Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API): @maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using"…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
19
votes
1 answer

Set ReSharper to put the 'using' import outside the namespace

I've ReSharper v9.0 installed on my VS. I also use StyleCop. I've disabled SA1200, so when I put the using statements outside the namespace, I don't get warned again. But when I add a reference via [alt]+[enter], which isn't available in using…
iroel
  • 1,760
  • 1
  • 18
  • 27
19
votes
6 answers

Bad practice? Non-canon usage of c#'s using statement

C# has the using statement, specifically for IDisposable objects. Presumably, any object specified in the using statement will hold some sort of resource that should be freed deterministically. However, it seems to me that there are many designs in…
snarf
  • 2,684
  • 1
  • 23
  • 26
18
votes
2 answers

What happens when 'return' is called from within a 'using' block?

If I have a method with a using block like this... public IEnumerable GetPersons() { using (var context = new linqAssignmentsDataContext()) { return context.Persons.Where(p => p.LastName.Contans("dahl")); …
Byron Sommardahl
  • 12,743
  • 15
  • 74
  • 131
17
votes
3 answers

Is it possible to extend the 'using' block in C#?

Is there a way to extend the using block in C# in such a way that takes a delegate as a second parameter alongside an IDisposable object and executes every time when an exception is thrown inside that using block? Imagine we have a delegate,…
user7450900
17
votes
5 answers

Does a C# using statement perform try/finally?

Suppose that I have the following code: private void UpdateDB(QuoteDataSet dataSet, Strint tableName) { using(SQLiteConnection conn = new SQLiteConnection(_connectionString)) { conn.Open(); using (SQLiteTransaction…
Kiril
  • 39,672
  • 31
  • 167
  • 226
16
votes
5 answers

How to determine whether a .NET exception is being handled?

We're investigating a coding pattern in C# in which we'd like to use a "using" clause with a special class, whose Dispose() method does different things depending on whether the "using" body was exited normally or with an exception. To the best of…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
16
votes
9 answers

Is there a list of common object that implement IDisposable for the using statement?

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you…
John B
  • 20,062
  • 35
  • 120
  • 170
16
votes
4 answers

C# exiting a using() block with a thread still running onthe scoped object

What happens to a thread if it is running a method in an object that was freed by exiting a using block? Example: using (SomeObject obj = new SomeObject ()) { obj.param = 10 ; Thread newThread = new Thread(() => { obj.Work();…
yitz
  • 171
  • 1
  • 7
15
votes
1 answer

why is there no Dispose method on HttpWebResponse

HttpWebReponse implements IDisposable interface, but why is there no Dispose method. It only contains Close method. Will be using pattern still available for this class?
user705414
  • 20,472
  • 39
  • 112
  • 155
1 2
3
29 30