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
15
votes
14 answers

When are C# "using" statements most useful?

So a using statement automatically calls the dispose method on the object that is being "used", when the using block is exited, right? But when is this necessary/beneficial? For example let's say you have this method: public void DoSomething() { …
John B
  • 20,062
  • 35
  • 120
  • 170
15
votes
3 answers

Why c# don't let to pass a using variable to a function as ref or out

Possible Duplicate: Passing an IDisposable object by reference causes an error? Why doesn't C# allow passing a variable from a using block to a function as ref or out? This is my code: using (Form s = new Form()) { doSomthing(ref s); } The…
Saleh
  • 2,982
  • 5
  • 34
  • 59
14
votes
7 answers

Implementing C++ equivalent of C# using statement

I am looking for an elegant solution for implementing the equivalent of the C# using statement in C++. Ideally the resultant syntax should be simple to use and read. C# Using statement details are here -…
adamretter
  • 3,885
  • 2
  • 23
  • 43
14
votes
4 answers

Identify IDisposable objects

i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable…
David Espart
  • 11,520
  • 7
  • 36
  • 50
14
votes
6 answers

Why would you use the using statement this way in ASP.NET?

Refactoring some code again. Seeing some of this in one of the ASP.NET pages: using (TextBox txtBox = e.Row.Cells[1].FindControl("txtBox") as TextBox) { } There is no need to dispose txtBox, because it's just a reference to an existing control. …
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
14
votes
1 answer

Using a null IDisposable value with the using statement

The following code produces no errors when executed: using ((IDisposable)null) { Console.WriteLine("A"); } Console.WriteLine("B"); Is a null value to using 'allowed'? If so, where is it documented? Most C# code I've seen will create a…
user2864740
  • 60,010
  • 15
  • 145
  • 220
14
votes
5 answers

Formatting/indentation for using statements (C#)

When it comes to using statements in C# (not to be confused with using directives that import namespaces), Visual Studio doesn't indent single-line code that follows if no braces are employed. This is typical of "nesting" using statements as shown…
JYelton
  • 35,664
  • 27
  • 132
  • 191
14
votes
2 answers

using on SQLDataReader

I know I asked a related question earlier. I just had another thought. using (SqlConnection conn = new SqlConnection('blah blah')) { using(SqlCommand cmd = new SqlCommand(sqlStatement, conn)) { conn.open(); // *** do I need…
xeshu
  • 788
  • 2
  • 11
  • 24
14
votes
3 answers

Closing SqlConnection and SqlCommand c#

In my DAL I write queries like this: using(SQLConnection conn = "connection string here") { SQLCommand cmd = new ("sql query", conn); // execute it blah blah } Now it just occurred to me that I am not explicitly closing the SQLCommand…
xeshu
  • 788
  • 2
  • 11
  • 24
14
votes
5 answers

Best practice for nested using statements?

I have a code block as follows and I'm using 3 nested using blocks. I found that using try finally blocks I can avoid this but if there are more than two using statements, what is the best approach? private FileStream fileStream = null; private…
not 0x12
  • 19,360
  • 22
  • 67
  • 133
14
votes
6 answers

Calling Dispose() vs when an object goes out scope/method finishes

I have a method, which has a try/catch/finaly block inside. Within the try block, I declare SqlDataReader as follows: SqlDataReader aReader = null; aReader = aCommand.ExecuteReader(); In the finally block, the objects which are manually…
csharpdev
  • 297
  • 2
  • 5
  • 9
13
votes
3 answers

"using" keyword for base class variable

I am going over the WildMagic 5 engine (www.geometrictools.com) where the Vector<> class is inheriting from a Tuple<> class which has an array of a particular size, named mTuple[] (set by a template parameter). So far so good, nothing special. In…
Samaursa
  • 16,527
  • 21
  • 89
  • 160
13
votes
2 answers

Use of Process with using block

Possible Duplicate: What happens if I don't close a System.Diagnostics.Process in my C# console app? As System.Diagnostics.Process inherits from Component which implements IDisposable, should I always create a Process with a using block? For…
g t
  • 7,287
  • 7
  • 50
  • 85
13
votes
3 answers

Why is using(null) a valid case in C#?

Could someone please explain to me why the code shown below is valid in C# and executes the call to Console.WriteLine? using (null) { Console.WriteLine ("something is here") } It compiles into (finally block is shown). As you can see compiler…
Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
13
votes
5 answers

The C# using statement, SQL, and SqlConnection

Is this possible using a using statement C# SQL? private static void CreateCommand(string queryString, string connectionString) { using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand…
user287745
  • 3,071
  • 10
  • 56
  • 99