using-statement

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() { using (Font font1 = new Font("Arial", 10.0f)) { // Draw some text here ...

How do I share IDisposable resources within a class?

I'm writing some console applications to move some data between SQLite databases. The classes for the connection and various prepared statements implenent IDisposable, so I'm instantiating these objects using using blocks, like this: using (SQLiteConnection sqlite_conn = new SQLiteConnection(connectionString)) { sqlite_conn.Open();...

List of cases where USING statement should be employed

"File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface. As a rule, when you use an IDisposable object, you should ...

C#: IEnumerator<T> in a using statement

I was curious to see how the SingleOrFallback method was implemented in MoreLinq and discovered something I hadn't seen before: public static T SingleOrFallback<T>(this IEnumerable<T> source, Func<T> fallback) { source.ThrowIfNull("source"); fallback.ThrowIfNull("fallback"); using (IEnumerator<T> iterator...

C# exiting a using() block with a thread still running onthe scoped object

What happens to a thread if it is running a method in an object that was freed by exiting a using block? Example: using (SomeObject obj = new SomeObject ()) { obj.param = 10 ; Thread newThread = new Thread(() => { obj.Work(); }); newThread.Start(); } ... obj.Work() is running on a new thread but ob...

Is there a list of common object that implement IDisposable for the using statement?

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket. Anyth...

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! ...

Bad practice? Non-canon usage of c#'s using statement

C# has the using statement, specifically for IDisposable objects. Presumably, any object specified in the using statement will hold some sort of resource that should be freed deterministically. However, it seems to me that there are many designs in programming which have a single, definite beginning and end, but lack intrinsic language ...

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 SqlTransaction? using (SqlConnection cn = new SqlConnection()) { using (SqlTransacti...

Removing redundant using statements

Does anyone know of a tool to remove redundant using statements from classes, or a whole solution? I'm using the Refactor! addin which has a "move type to separate file" smart tag, but it takes all the using clauses from the original class with it. ...

Best practice regarding returning from using blocks

Which way is better practice: return a value from a method inside an using statement or declare a variable before, set it inside and return it after? public int Foo() { using(..) { return bar; } } or public int Foo() { var b = null; using(..) { b = bar; } return b; } ...

C# - Location of Using Statements

One thing I have noticed a lot of back and forth on is where using statements should be placed in a C# code file- whether its in the outermost scope or inside a namespace. I understand that the location of the using statement affects the scope of the references within that file, but what I don't understand is why, in most cases, someone ...

Using Statement with more than one system resource

I have used the "using" statement in both C# and VB. I agree with all the critics regarding nesting using statements (C# seems well done, VB not so much) So with that in mind I was interested in improving my VB using statements by "using" more than one system resource within the same block: Example: Using objBitmap as New Bitmap(100,...

Using all overloads of the base class

When a subclass overrides a baseclass's method, all of the baseclass's overloads are not available from the subclass. In order to use them there should be added a using BaseClass::Method; line in the subclass. Is there a quick way to inheirt the baseclass's overloads for ALL of the overridden methods? (not needing to explicitly specify ...

using namespace issue

When I use the following #include <map> using namespace LCDControl; Any reference to the std namespace ends up being associated with the LCDControl name space. For instance: Generic.h:249: error: 'map' is not a member of 'LCDControl::std' How do I get around this? I didn't see anything specific to this on any documentation I look...

Does HttpResponse work in a "using" block with out a explicit response.close()

Hi all, I was trying to get clarification on this : Method-1: Dim request = CreateRequest(uri) //some uri Dim response = DirectCast(request.GetResponse, HttpWebResponse) response.Close() Method-2: Dim request = Createrequest(uri) Using response = DirectCast(request.GetResponse, HttpWebResponse) End Using When I used both Method-1...

Easily select default usings for project namespaces and subamespaces

Is there an easy way in Visual Studio to assign default usings when i make a new file in a certain (sub)namespace in my project? ...

Visual Studio 2008 automatically add namespces like Eclipse

Does VS2008 have a feature akin to Eclipse's ability to automatically add import declarations for undefined namespaces? ...

Why doesn't 'using' have a catch block?

I understand the point of "using" is to guarantee that the Dispose method of the object will be called. But how should an exception within a "using" statement be handled? If there is an exception, I need to wrap my "using" statement in a try catch. For example: Lets say there is an exception created in the creation of the object insi...

How can disposable class detect whether there is an exception in progress?

I have a class that implements IDisposable public class Foo: IDisposable { public void Dispose() { // do the disposing } } Then I have a method that uses the class in the following manner: void Bar() { using (var f = new Foo()) { // do whatever } } When the code leaves the using {...} boundary, the ...