dispose

using on SQLDataReader

天大地大妈咪最大 提交于 2019-11-27 02:18:07
问题 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 to put this in using as well? *** SqlDataReader dr = cmd.ExecuteReader() { While(dr.Read()) { //read here } } } } The argument is that: Since the SqlDataReader dr object is NOT A NEW OBJECT LIKE THE connection or command objects, its simply a reference pointing to the cmd

Clear controls does not dispose them - what is the risk?

北城以北 提交于 2019-11-27 02:10:20
There are multiple threads( a , b , c etc.) about the fact that Clear () ing items in the .NET component containers does not Dispose them(by calling Dispose( true ). Most frequently, IMHO, the Clear-ed components are not used anymore in the application, so it needs explicitly be Disposed after Clearing them from the parent containers. Maybe is a good idea that collection's Clear method had a bool parameter dispose that when in true also disposes the collection elements before its removing from the list? Asking for modifications like this is pointless, the Windows Forms team has been disbanded

How and when are c# Static members disposed?

旧巷老猫 提交于 2019-11-27 01:46:23
问题 I have a class with extensive static members, some of which keep references to managed and unmanaged objects. For instance, the static constructor is called as soon as the Type is referenced, which causes my class to spin up a blockingQueue of Tasks. This happens when one of the static methods is called, for example. I implemented IDisposable, which gives me methods to handle disposal on any instance objects I created. However, these methods are never called if the consumer doesn't create any

Minimal IDisposable implimenation for managed resources only

僤鯓⒐⒋嵵緔 提交于 2019-11-26 23:20:58
问题 There is a LOT of info around about the "standard full" IDisposable implementation for disposing of unmanaged resources - but in reality this case is (very) rare (most resources are already wrapped by managed classes). This question focuses on a mimimal implementation of IDisposable for the much more common "managed resources only" case. 1: Is the mimimal implementation of IDisposable in the code below correct, are there issues? 2: Is there any reason to add a full standard IDisposable

Disposable singleton in C#

僤鯓⒐⒋嵵緔 提交于 2019-11-26 22:52:14
问题 I have a singleton that uses the "static readonly T Instance = new T();" pattern. However, I ran into a case where T is disposable, and actually needs to be disposed for unit tests. How can I modify this pattern to support a disposable singleton? The interface I would like is something like: var x = Foo.Instance; var y = Foo.Instance; // x == y ... x.Release(); // this causes the next Foo.Instance to return a fresh object // also, it assumes no further operations on x/y will be performed.

Is it possible to force the use of “using” for disposable classes?

元气小坏坏 提交于 2019-11-26 22:50:12
问题 I need to force the use of "using" to dispose a new instance of a class. public class MyClass : IDisposable { ... } using(MyClass obj = new MyClass()) // Force to use "using" { } 回答1: The fact that you need to ensure that the object is disposed indicates a design flaw. It's fine if disposing is the polite or efficient thing to do, but it should not be semantically necessary . There is no way to enforce that an object is disposed of via the using statement. However, what you can do is maintain

Does garbage collector call Dispose()? [duplicate]

荒凉一梦 提交于 2019-11-26 22:31:47
This question already has an answer here: Will the Garbage Collector call IDisposable.Dispose for me? 9 answers I thought the GC would call Dispose eventually if your program did not but that you should call Dispose() in your program just to make the cleanup deterministic. However, from my little test program, I don't see Dispose getting called at all.... public class Test : IDisposable { static void Main(string[] args) { Test s = new Test(); s = null; GC.Collect(); Console.ReadLine(); } public Test() { Console.WriteLine("Constructor"); } public void Dispose() { Console.WriteLine("Dispose"); }

How to dispose a class in .net?

不打扰是莪最后的温柔 提交于 2019-11-26 22:14:54
问题 The .NET garbage collector will eventually free up memory, but what if you want that memory back immediately? What code do you need to use in a class MyClass to call MyClass.Dispose() and free up all the used space by variables and objects in MyClass ? 回答1: IDisposable has nothing to do with freeing memory. IDisposable is a pattern for freeing unmanaged resources -- and memory is quite definitely a managed resource. The links pointing to GC.Collect() are the correct answer, though use of this

Datatable.Dispose() will make it remove from memory?

故事扮演 提交于 2019-11-26 21:49:14
问题 I have researching through very simple code and get stuck on seeing the dispose() result of datatable Following is the code DataTable dt= new Datatable(); SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon); SqlCommand.CommandType= CommandType.StroedProcedure; SqlCon.Open(); sqlDataReader dr= cmd.ExecuteReader(); dt.Load(dr); SqlCon.Close(); grdView.DataSource =dt; dt.Dispose() // Here I dispose the table as it is no use for me & wanna memory free from this But after disposing off the

Safely disposing Excel interop objects in C#?

走远了吗. 提交于 2019-11-26 20:56:21
问题 i am working on a winforms c# visual studio 2008 application. the app talks to excel files and i am using Microsoft.Office.Interop.Excel; to do this. i would like to know how can i make sure that the objects are released even when there is an error? here's my code: private void button1_Click(object sender, EventArgs e) { string myBigFile=""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult