idisposable

How should I handle exceptions in my Dispose() method?

浪尽此生 提交于 2019-12-06 17:33:42
问题 I'd like to provide a class to manage creation and subsequent deletion of a temporary directory. Ideally, I'd like it to be usable in a using block to ensure that the directory gets deleted again regardless of how we leave the block: static void DoSomethingThatNeedsATemporaryDirectory() { using (var tempDir = new TemporaryDirectory()) { // Use the directory here... File.WriteAllText(Path.Combine(tempDir.Path, "example.txt"), "foo\nbar\nbaz\n"); // ... if (SomeCondition) { return; } if

What happens when 'return' is called from within a 'using' block? [duplicate]

穿精又带淫゛_ 提交于 2019-12-06 17:06:05
问题 This question already has answers here : returning in the middle of a using block (7 answers) Closed 6 years ago . If I have a method with a using block like this... public IEnumerable<Person> GetPersons() { using (var context = new linqAssignmentsDataContext()) { return context.Persons.Where(p => p.LastName.Contans("dahl")); } } ...that returns the value from within the using block, does the IDisposable object still get disposed? 回答1: Yes it does. The disposing of the object occurs in a

How does GC and IDispose work in C#?

跟風遠走 提交于 2019-12-06 13:35:08
问题 I remember i was loading in images by streaming it from the net straight into a bitmap. close the stream, return the bitmap and held it in an image control. I excepted when i did = loadPicture() the first bitmap would be freed like a smart pointer would do in C++. But it didnt and i consumed a lot of ram until i called dispose. So my question is. How does the GC and Dispose able objects work in C#? and why isnt it implemented like a smart_ptr? 回答1: References are not smart pointers. Letting a

Does a class need to implement IDisposable when all members are explicitly disposed?

眉间皱痕 提交于 2019-12-06 11:16:13
Trying to understand when implementation of IDisposable is necessary: I wrote a little example. public class FileManager { private FileStream fileStream; public void OpenFile(string path) { this.fileStream = File.Open(path, FileMode.Open, FileAccess.Read); } public void CloseFile(string path) { if ( this.fileStream != null && this.fileStream.CanRead) { this.fileStream.Close(); } this.fileStream.Dispose(); } } // client var manager = new FileManager(); manager.Open("path"); manager.Close("path"); Does this class need to implement IDisposable because it has a managed resource (FileStream) which

Should Linq to SQL repository implement IDisposable

旧时模样 提交于 2019-12-06 08:54:38
问题 I've been googling a ton on repository patterns with Linq over the last few days. There's a lot of info out there but it's often contradictory and I'm still looking for a definitive source. One of the things I'm still not sure about is whether the repository should instantiate it's own DataContext and have a SubmitChanges method, or if the DataContext should be injected and the submission handled externally. I've seen both designs but no real comment on the reasoning. Anyway, the following

IDisposable member of WPF Window class

自作多情 提交于 2019-12-06 04:45:03
When I add IDisposable class member to Windows Forms Form class, I add disposing code to Form's Dispose method. What should I do when I add IDisposable class member to WPF Window class, which is not IDisposable? Extend your window class so that it has IDisposable, then implement the Dispose() method as before: public class MyWindow : Window, IDisposable { public void Dispose() { // Dispose your objects here as before. } } Approaches you can use: Use Closed event on Window . Implement IDisposable interface yourself for this Window . You could implement the IDisposable pattern that hooks into

IDisposable, Finalizers and the definition of an unmanaged resource

佐手、 提交于 2019-12-06 03:07:15
问题 I'm trying to make sure that my understanding of IDisposable is correct and there's something I'm still not quite sure on. IDisposable seems to serve two purpose. To provide a convention to "shut down" a managed object on demand. To provide a convention to free "unmanaged resources" held by a managed object. My confusion comes from identifying which scenarios have "unmanaged resources" in play. Say you are using a Microsoft-supplied IDisposable -implementing (managed) class (say, database or

DRY IDisposable Pattern

我是研究僧i 提交于 2019-12-06 03:04:51
A lot of my classes repeat the below code to implement IDisposable. This seems to violate the DRY (Don't Repeat Yourself) principle. I could avoid some of the work by creating an AbstractDisposable base class, but that seems inappropriate / wouldn't work if I needed to extend other existing objects (assuming those objects weren't themselves disposable). Another option would be to use a template/meta language where I could specify lists of managed and unmanaged resources for each class and have the generic Dispose Pattern auto generated when I build my project - but so far I've not played with

How does this class implement IDisposable if it doesn't have a Dispose method?

爱⌒轻易说出口 提交于 2019-12-06 02:44:32
问题 FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. How is that possible? 回答1: It does have the Dispose method through inheritance, but it is an explicit implementation. To call it, you would have to use ((IDisposable)myObject).Dispose(); Or, of course, just wrap it in a using block, as it does the explicit call for you. 回答2: Its implemented in the base class WebResponse, see http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx 回答3: When you

Memory release with IDisposable and without IDisposable

心已入冬 提交于 2019-12-06 01:50:19
In my app I have a large object that's created every few seconds. I do with it some job and then I don't need it anymore. I saw in the task manager that the ram size goes up even if I don't have any reference to the object and it needs to be collected. After implementing IDisposable the ram goes down immediately. Why is this? I didn't do GC.Collect , I just released the object and told the GC it doesn't need to call the finalizer for my object. EDIT: Here is the code I use for my IDisposable objects: public void Dispose() { base.Dispose(); Dispose(true); GC.SuppressFinalize(); } private void