dispose

Is SqlCommand.Dispose() required if associated SqlConnection will be disposed?

泪湿孤枕 提交于 2019-11-26 20:46:32
I usually use code like this: using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) { var command = connection.CreateCommand(); command.CommandText = "..."; connection.Open(); command.ExecuteNonQuery(); } Will my command automatically disposed? Or not and I have to wrap it into using block? Is it required to dispose SqlCommand ? Just do this: using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString)) using(var command = connection.CreateCommand()) { command.CommandText = "..."; connection

Right way to dispose Image/Bitmap and PictureBox

爱⌒轻易说出口 提交于 2019-11-26 20:27:01
I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls or whatever I want. There are two things I am doing. Drawing custom shapes and loading bitmaps from .png files. The next line locks the file when loading (which is an undesired scenario): Bitmap bmp = new Bitmap("file.png"); So I am using another way to load bitmap. public static Bitmap LoadBitmap(string path) { using (Bitmap original = new Bitmap(path)) { return new Bitmap(original); } } This is I guess much slower, but I

returning in the middle of a using block

旧街凉风 提交于 2019-11-26 20:10:29
Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? As several others have pointed out in general this is not a problem. The only case it will cause you issues is if you return in the middle of a using statement and additionally return the in using variable. But then again, this would also cause you issues even if you didn't return and simply kept a reference to a variable. using ( var x = new Something() ) { // not a good idea return x; } Just as bad Something y; using ( var x

C# USING keyword - when and when not to use it?

北战南征 提交于 2019-11-26 19:58:54
问题 I'd like to know when i should and shouldn't be wrapping things in a USING block. From what I understand, the compiler translates it into a try/finally, where the finally calls Dispose() on the object. I always use a USING around database connections and file access, but its more out of habit rather than a 100% understanding. I know you should explicity (or with a using) Dispose() objects which control resources, to ensure they are released instantly rather than whenever the CLR feels like it

What best practices for cleaning up event handler references?

半腔热情 提交于 2019-11-26 19:12:05
问题 Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses(); Session.AllUnitsReady -= Session_AllUnitsReady; Session.AllUnitsResultsPublished -= Session_AllUnitsResultsPublished; Session.UnitFailed -= Session_UnitFailed; Session.SomeUnitsFailed -= Session_SomeUnitsFailed; Session.UnitCheckedIn -= Session_UnitCheckedIn; UnattachListeners(); } The purpose being to clean up all event subscriptions that we have registered for on the target (Session) so that

Dispose, when is it called?

房东的猫 提交于 2019-11-26 18:55:13
Consider the following code: namespace DisposeTest { using System; class Program { static void Main(string[] args) { Console.WriteLine("Calling Test"); Test(); Console.WriteLine("Call to Test done"); } static void Test() { DisposeImplementation di = new DisposeImplementation(); } } internal class DisposeImplementation : IDisposable { ~DisposeImplementation() { Console.WriteLine("~ in DisposeImplementation instance called"); } public void Dispose() { Console.WriteLine("Dispose in DisposeImplementation instance called"); } } } The Dispose just never get's called, even if I put a wait loop after

When should I dispose my objects in .NET?

只谈情不闲聊 提交于 2019-11-26 18:09:27
问题 For general code, do I really need to dispose an object? Can I just ignore it for the most part or is it a good idea to always dispose an object when your 100% sure you don't need it anymore? 回答1: Dispose of an object the instant your are done with it. Disposable objects represent objects holding a valuable resource which the CLR is not intrinsically aware of. Consequently the GC is also unaware of the resources and is unable to make intelligent decisions as to when it should collect a

What happens if I don't call Dispose on the pen object?

拈花ヽ惹草 提交于 2019-11-26 17:42:45
What happens if I don't call Dispose on the pen object in this code snippet? private void panel_Paint(object sender, PaintEventArgs e) { var pen = Pen(Color.White, 1); //Do some drawing } jason The Pen will be collected by the GC at some indeterminate point in the future, whether or not you call Dispose . However, any unmanaged resources held by the pen (e.g., a GDI+ handle) will not be cleaned up by the GC. The GC only cleans up managed resources. Calling Pen.Dispose allows you to ensure that these unmanaged resources are cleaned up in a timely manner and that you aren't leaking resources.

What is the difference between using IDisposable vs a destructor in C#?

元气小坏坏 提交于 2019-11-26 17:26:08
问题 When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point. My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the garbage collector to do it. Is this correct? Does that mean I should always explicitly call Dispose on an object? What are some common examples of this? 回答1: A finalizer (aka destructor) is part of garbage collection (GC) - it is indeterminate when

Determining if IDisposable should extend an interface or be implemented on a class implementing said interface

落花浮王杯 提交于 2019-11-26 17:11:18
问题 How can I determine if I should extend one of my interfaces with IDisposable or implement IDisposable on a class that implements my interface? I have an interface that does not need to dispose of any external resources, except for one particular implementation. My options seem to be: 1) Implement IDisposable on the interface requiring all of the implementations to implement Dispose, even if only an empty method. -or- 2) Implement IDisposable on only the classes that have resources needing to