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
-2
votes
4 answers

Save uploaded file - using

I've uploaded a file with the FileUpload control. I've got the path and everything and I would like to save the image to the server. I don't want to use the SaveAs() method. I wonder, is there a way to save the file with using-statement?
user1121487
  • 2,662
  • 8
  • 42
  • 63
-2
votes
2 answers

'Using static' on a class contained private enum in C#

Consider the following class Body { [Flags] enum Organs { None = 0, Brain = 1, Heart = 2, Kidney = 4, Skin = 8 } Organs damagedOrgan = Organs.Heart; Organs currentOrgan = Organs.Kidney; Organs favoriteOrgan = Organs.Brain; …
jsmars
  • 1,640
  • 5
  • 21
  • 33
-2
votes
2 answers

Will I still get the Dispose advanges, if I in a using, initialize with null?

I would like to refactor my SqlDataReader code, so it's using using.. SqlDataReader reader = null; reader = xdCmd.ExecuteReader(); // use reader.. Can I use solution 1) where I declare the reader in the using and first later initialize it with a…
radbyx
  • 9,352
  • 21
  • 84
  • 127
-2
votes
1 answer

"using static" throwing error

I'm trying to use using static System.Console; rather than using System; so I only have to type out WriteLine("bla") vs Console.WriteLine("bla"). My code is as follows: using static System.Console; public class Program { public static void…
georgie__
  • 1
  • 3
-2
votes
1 answer

What's the meaning of this code? (Appears at the top of a Windows Form Application page)

I'm an A2 level computing student and I'm creating a program for my project which (using c#) takes data from an MDF and displays it in windows form textboxes. Because I'm a totally inexperienced programmer so used an online guide…
-2
votes
3 answers

What does using ensure in c#?

I read this documentation but I'm still confused. using (Font font1 = new Font("Arial", 10.0f)) { byte charset = font1.GdiCharSet; } In the code, does it mean that we are introducing a new instance of Font class called font1. However, this…
Aleksei Nikolaevich
  • 325
  • 3
  • 15
  • 40
-3
votes
2 answers

C# Dispose resources on exceptions without using

I'm writing a class library which includes socket operations. I don't want to rely on the consumer to dispose resources after finishing or when an exception is thrown. Normally I would use the "using" statement, but this doesn't work outside of a…
Josh
  • 287
  • 1
  • 8
-3
votes
2 answers

Any way to have a `using` block that doesn't define a scope block?

using(var myDisposable = new MyDisposable) { //Do stuff. } is great and all. But if what you wanted was: using(var myDisposable = new MyDisposable) { var myAnswer = CalculateMyAnswer(myDisposable); } and you wanted to use myAnswer later,…
Brondahl
  • 7,402
  • 5
  • 45
  • 74
-3
votes
1 answer

How C# handles workload with using statement?

I am currently cleaning up the code for my project and trying to improve its performance. As a part of improvement, I thought of removing unused "using" statements from all files. I would like to figure out if that would indeed improve the…
Vadzim Savenok
  • 930
  • 3
  • 14
  • 37
-4
votes
1 answer

Scope of using block on an external variable

What happens to the image in "rendered" once using block disposes "b"? Bitmap rendered; using(Bitmap b = new Bitmap(calcHeight, calcWidth)) { using (Graphics g = Graphics.FromImage(b)) { RenderMyBitMap(ref b); rendered = b; …
-7
votes
4 answers

using if statement under using case

this says need to return path but i cant reach if cases is there any way ? thanks public IEnumerable GetRequests(string erisim, string sube, string sicil) { using (var redisclient = RedisManager.GetClient()) { var…
1 2 3
29
30