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
52
votes
8 answers

Why use a using statement with a SqlTransaction?

I've been running into some problems concerning a SqlTransaction I'm using in my code. During my Googling I see many people using a using statement with a SqlTransaction. What is the benefit and/or difference of using this type of statement with a…
50
votes
11 answers

C# "Using" Syntax

Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff } If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it?
Gordon Thompson
  • 4,764
  • 8
  • 48
  • 62
49
votes
4 answers

How to handle WCF exceptions (consolidated list with code)

I'm attempting to extend this answer on SO to make a WCF client retry on transient network failures and handle other situations that require a retry such as authentication expiration. Question: What are the WCF exceptions that need to be handled,…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
49
votes
10 answers

C# conditional using block statement

I have the follow code but it is awkward. How could I better structure it? Do I have to make my consuming class implement IDisposable and conditionally construct the network access class and dispose it when I am done? protected void…
superlogical
  • 14,332
  • 9
  • 66
  • 76
48
votes
4 answers

What scope does a using statement have without curly braces

I inherited the following code: using (var dataAccessConnection = da.GetConnection()) //no opening curly brace here using (var command = new SqlCommand(sql, dataAccessConnection.Connection)) { command.CommandType =…
Mathias F
  • 15,906
  • 22
  • 89
  • 159
45
votes
2 answers

MemoryStream in Using Statement - Do I need to call close()

When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here? using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); }
AJM
  • 32,054
  • 48
  • 155
  • 243
41
votes
12 answers

Dealing with .NET IDisposable objects

I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable, which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't…
Atario
  • 1,371
  • 13
  • 24
34
votes
3 answers

Where do I put try/catch with "using" statement?

Possible Duplicate: try/catch + using, right syntax I would like to try/catch the following: //write to file using (StreamWriter sw = File.AppendText(filePath)) { sw.WriteLine(message); } Do I put the try/catch blocks inside the using…
Ryan R
  • 8,342
  • 15
  • 84
  • 111
34
votes
4 answers

When is "using" block used for in C#? How to use "using" block in C#?

I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new…
Omer
  • 8,194
  • 13
  • 74
  • 92
34
votes
3 answers

Does "using" statement always dispose the object?

Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { //…
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
32
votes
6 answers

Why can't I reference System.Runtime.Serialization.Json in C#

I want to use an API to get info from the interwebz. The API returns data in Json format. I'm running Microsoft Visual Studio C# 2010 Express addition. It appears that I have the .NET Framework 4 Client Profile set as my "Target framework" but I'm…
Methodician
  • 2,396
  • 5
  • 30
  • 49
32
votes
2 answers

EF (entity framework) usage of "using" statement

I have a project on MVC. We chose EF for our DB transactions. We created some managers for the BLL layer. I found a lot of examples, where "using" statement is used, i.e. public Item GetItem(long itemId) { using (var db = new…
Pal
  • 756
  • 2
  • 10
  • 20
31
votes
2 answers

Nested using statements

As Eric Gunnerson shows in this blog post, in C# you can nest using statements as: using (StreamWriter w1 = File.CreateText("W1")) using (StreamWriter w2 = File.CreateText("W2")) { // code here } Is there a similar way to do it in VB.Net? I…
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
30
votes
9 answers

Is C#'s using statement abort-safe?

I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138), using…
Dzinx
  • 55,586
  • 10
  • 60
  • 78
30
votes
4 answers

what does a using statement without variable do when disposing?

I've always used using with variable and assignment. Now i have like this a class DbProviderConnection: public class DbProviderConnection : IDisposable { public DbConnection Connection { get; set; } public DbTransaction Transaction { get;…
char m
  • 7,840
  • 14
  • 68
  • 117
1
2
3
29 30