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

The type or namespace name could not be found

I have a C# solution with several projects in Visual Studio 2010. One is a test project (I'll call it "PrjTest"), the other is a Windows Forms Application project (I'll call it "PrjForm"). There is also a third project referenced by PrjForm, which…
Anders
  • 15,227
  • 5
  • 32
  • 42
365
votes
2 answers

using statement with multiple variables

Is it possible to make this code a little more compact by somehow declaring the 2 variable inside the same using block? using (var sr = new StringReader(content)) { using (var xtr = new XmlTextReader(sr)) { obj =…
Antony Scott
  • 21,690
  • 12
  • 62
  • 94
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
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
228
votes
9 answers

try/catch + using, right syntax

Which one: using (var myObject = new MyClass()) { try { // something here... } catch(Exception ex) { // Handle exception } } OR try { using (var myObject = new MyClass()) { // something here... …
Xaqron
  • 29,931
  • 42
  • 140
  • 205
225
votes
7 answers

returning in the middle of a using block

Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it?
tafa
  • 7,146
  • 3
  • 36
  • 40
124
votes
5 answers

What happens if i return before the end of using statement? Will the dispose be called?

I've the following code using(MemoryStream ms = new MemoryStream()) { //code return 0; } The dispose() method is called at the end of using statement braces } right? Since I return before the end of the using statement, will the…
NLV
  • 21,141
  • 40
  • 118
  • 183
124
votes
12 answers

Does Java have a using statement?

Does Java have a using statement that can be used when opening a session in hibernate? In C# it is something like: using (var session = new Session()) { } So the object goes out of scope and closes automatically.
mrblah
  • 99,669
  • 140
  • 310
  • 420
121
votes
3 answers

Does Dispose still get called when exception is thrown inside of a using statement?

In the example below, is the connection going to close and disposed when an exception is thrown if it is within a using statement? using (var conn = new SqlConnection("...")) { conn.Open(); // stuff happens here and exception is…
Brian Kim
  • 24,916
  • 6
  • 38
  • 26
111
votes
1 answer

Do using statements and await keywords play nicely in c#

I have a situation where I am making an async call to a method that returns and IDisposable instance. For example: HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.google.com")); Now before async was on the scene, when…
swingdoctor
  • 1,551
  • 2
  • 11
  • 18
107
votes
5 answers

Can "using" with more than one resource cause a resource leak?

C# lets me do the following (example from MSDN): using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f)) { // Use font3 and font4. } What happens if font4 = new Font throws? From what I understand font3 will…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
89
votes
15 answers

'using' statement vs 'try finally'

I've got a bunch of properties which I am going to use read/write locks on. I can implement them either with a try finally or a using clause. In the try finally I would acquire the lock before the try, and release in the finally. In the using…
Jeremy
  • 44,950
  • 68
  • 206
  • 332
87
votes
3 answers

Will a using statement rollback a database transaction if an error occurs?

I've got an IDbTransaction in a using statement but I'm unsure if it will be rolled back if an exception is thrown in a using statement. I know that a using statement will enforce the calling of Dispose()...but does anyone know if the same is true…
mezoid
  • 28,090
  • 37
  • 107
  • 148
76
votes
3 answers

How to use an iterator?

I'm trying to calculate the distance between two points. The two points I stored in a vector in C++: (0,0) and (1,1). I'm supposed to get results as 0 1.4 1.4 0 But the actual result that I got is 0 1 -1 0 I think there's something wrong with the…
user188276
55
votes
8 answers

Can the C# using statement be written without the curly braces?

I was browsing a coworkers c# code today and found the following: using (MemoryStream data1 = new MemoryStream()) using (MemoryStream data2 = new MemoryStream()) { // Lots of code.......... } I had always seen the using…
DaveB
  • 9,470
  • 4
  • 39
  • 66
1
2 3
29 30