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
-1
votes
1 answer

How can I prevent this code from disposing objects multiple times?

When I run a Code Analysis against the following code: Protected Function GetOrderEntry() As IList(Of OE) Dim results As IList(Of OE) = New List(Of OE)() Using connection As IDbConnection =…
-1
votes
2 answers

Using keyword and Managed\UnManaged code

C# keyword Using implements Idisposable which provides a mechanism for releasing unmanaged resources. Now i was going through this code string txt = String.Empty; using (StreamReader sr = new StreamReader(filename)) { txt =…
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
-1
votes
1 answer

Visual Studio not finding reference in Class Library

I'm building a class library with some utility functions that any of my projects can access. I need to be able to use some classes within the System.Net namespace, like WebHeaderCollection. Unfortunately, Visual Studio doesn't find them, and doesn't…
silkfire
  • 24,585
  • 15
  • 82
  • 105
-1
votes
2 answers

System.Environment and System.Threading.Thread - Using directive is unneccesary

I am getting " Using directive is unneccesary" error on these two. What is the correct way to use them ?
user198989
  • 4,574
  • 19
  • 66
  • 95
-1
votes
1 answer

Performance comparison between using block and Dispose method

Think about these 2 snippets: Approach 1: use using statement using(var connection = new SqlConnection()) using(var command = new SqlCommand(cmdText, connection)){ try{ connection.Open(); using(var reader =…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
-1
votes
1 answer

Invalid Parameters Execption using sequence

I have the following code (where res.shuffle is an image, ein.Shuffle is a bool and kontrast is the kontrast-color of BackColor): using (Bitmap img = Code.EditImageColor(res.shuffle, (ein.Shuffle ? BackColor : kontrast))) { pictureBox1.Image =…
cramopy
  • 3,459
  • 6
  • 28
  • 42
-1
votes
1 answer

Handling IDisposable object without the using statement

I'm sure this is an easy question, but what am I supposed to do when handling an IDisposable object without the using statement?
Phate01
  • 2,499
  • 2
  • 30
  • 55
-1
votes
2 answers

C# database connection use of USING or not?

I tried to find my answer on google as always but I found only a partial answer to my question. My question is: while creating a connection to a database, what is the more preferable way to make it: without or with the USING. For now, as I start in…
MindKind
  • 49
  • 1
  • 12
-1
votes
2 answers

should use using/end using or manually close sqlConnection

in the following methods which is the suitable to work with SQL Method 1 Using conn As New SqlConnection("....") conn.Open() '/to do End Using Method 2 Try dim conn as new sqlconnection =("....") conn.open() '/to do Catch …
friend
  • 41
  • 1
  • 2
  • 6
-1
votes
1 answer

Dispose behavior when re instancing within using block?

I'm wondering what (and if it's defined) the behavior is if i need to re instanciate a variable within it's own using() tag? I have an actual use case for this, using SharpDX i have variables that i want declared outside the render loop (used for…
Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
-1
votes
1 answer

What is the correct way use of "using" in C#.NET?

which approaches are correct about "using" (first or second)? First: using (DataTable dt = list.ToDataTable()) { dataList.DataSource = dt; dataList.DataBind(); } Second: using (DataTable dt = list.ToDataTable()) { …
x19
  • 8,277
  • 15
  • 68
  • 126
-1
votes
1 answer

I need the using-statement to lock disposing the resource, is my custom solution robust?

I am in a situation where cleaning up unmanaged resources is a critical section. To solve this, I changed this... void SomeMethod() { //work using (var doc = SpreadsheetDocument.Open(results.FileName, true)) { //use doc. …
Onosa
  • 1,275
  • 1
  • 12
  • 28
-1
votes
3 answers

Using the using statement multiple times

I have a connection to a database on my asp.net page, where I am using the "using statement" many times. I am using it once for every method that uses the connection. My wish is to reduce these statements to only one, both for performance reasons…
Nurp
  • 1,409
  • 13
  • 25
-1
votes
3 answers

Use of C# using keyword in a user defined class

Everybody knows that using keyword is used when we want to clean up the unmanaged resources. If the class implements IDisposable then we can use using keyword with the object of that class. But if I want to use using keyword with the object of my…
Puneet Pant
  • 918
  • 12
  • 37
-1
votes
3 answers

Returning from method disposes corectly the object?

If you use the using method instead of lets say FileStream.Close();, will the class dispose correctly? private static string GetString() { using(FileStream fs = new FileStream("path", FileMode.Open)) using(StreamReader sr = new…
Thanatos
  • 1,176
  • 8
  • 18
1 2 3
29
30