idisposable

Disposing an ImageList

柔情痞子 提交于 2019-12-07 11:30:21
问题 What's the appropriate way to dispose an ImageList object? Suppose I have some class with a private ImageList imageList member. Now, at some moment I perform the following code: // Basically, lazy initialization. if (imageList == null) { imageList = new ImageList(); Image[] images = Provider.CreateImages(...); foreach (var image in images) { // Does the 'ImageList' perform implicit copying here // or does it aggregate a reference? imageList.Images.Add(image); // Do I need to do this? //image

Question on IDisposable

人走茶凉 提交于 2019-12-07 11:26:15
问题 My question is why do I need IDisposable? I have a class that consumes some resources that need to be freed. I have two options Without IDisposable class SomeUtilityClass { public Stop() { // free resources } } With IDisposable class SomeUtilityClass, IDisposable { public void Dispose() { // free resources } } So why do I need IDisposable here? It does not matther how to name the function. class Program { public Main(..) { SomeUtilityClass _class = new SomeUtilityClass(); // clean up when we

Using using to dispose of nested objects

核能气质少年 提交于 2019-12-07 06:57:49
问题 If I have code with nested objects like this do I need to use the nested using statements to make sure that both the SQLCommand and the SQLConnection objects are disposed of properly like shown below or am I ok if the code that instantiates the SQLCommand is within the outer using statement. using (var conn = new SqlConnection(sqlConnString)) { using (var cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = cmdTextHere; conn.Open(); cmd.Connection = conn;

Where to dispose resources in a System.Windows.Form-derived class?

南楼画角 提交于 2019-12-07 06:00:13
问题 I've got a form that creates a couple of disposable resources in its constructor that I need to dispose of. However, the C# form designer already produces a Dispose() method in the Designer.cs file that doesn't appear to have any type of user hook in it. So I'm at a loss as to how I'm supposed to implement the typical IDisposable pattern. This form is occasionally created but never shown, so using the Close event won't help. The objects in question are not IComponents , so I can't just add

The primary use of IDisposable interface [duplicate]

柔情痞子 提交于 2019-12-07 03:04:17
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Proper use of the IDisposable interface "IDisposable Interface" article tells: The primary use of this interface is to release unmanaged resources Why? Why only unmanaged? Whole my life I thought its PRIMIRALY use is to release ANY resources: managed (connections to DBs, services proxies, etc) and unmanaged (if they are used in application). P.S. I believe there are already questions on this topic, but can't

I don't quite understand the workings of using/Disposable objects

折月煮酒 提交于 2019-12-07 02:33:51
问题 I asked a question regarding returning a Disposable (IDisposable) object from a function, but I thought that I would muddle the discussion if I raised this question there. I created some sample code: class UsingTest { public class Disposable : IDisposable { public void Dispose() { var i = 0; i++; } } public static Disposable GetDisposable(bool error) { var obj = new Disposable(); if (error) throw new Exception("Error!"); return obj; } } I coded it this way deliberately, because then I call:

Throwing exception in finalizer to enforce Dispose calls:

吃可爱长大的小学妹 提交于 2019-12-07 02:17:04
问题 Here is the typical IDisposable implementation that I believe is recommended: ~SomeClass() { Dispose(false); } public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } protected virtual void Dispose(bool isDisposing) { if(isDisposing) { // Dispose managed resources that implement IDisposable. } // Release unmanaged resources. } Now, to my understanding, the idea behind the finalizer there is that if I don't call Dispose, my unmanaged resources will be released properly still.

CA1001 Visual Studio 2012 Code Analysis warning. What does it mean?

谁说胖子不能爱 提交于 2019-12-07 02:15:02
问题 It is not that important but I am trying to figure out what it is telling me and is it a legitimate warning ? Can someone explain this error in simple terms for me ? CA1001 Types that own disposable fields should be disposable Implement IDisposable on 'MemVoteManager' because it creates members of the following IDisposable types: 'CongressDBEntities'. If 'MemVoteManager' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to

Why disposing StreamReader makes a stream unreadable? [duplicate]

限于喜欢 提交于 2019-12-06 20:33:56
问题 This question already has answers here : Can you keep a StreamReader from disposing the underlying stream? (8 answers) Closed 4 years ago . I need to read a stream two times, from start to end. But the following code throws an ObjectDisposedException: Cannot access a closed file exception. string fileToReadPath = @"<path here>"; using (FileStream fs = new FileStream(fileToReadPath, FileMode.Open)) { using (StreamReader reader = new StreamReader(fs)) { string text = reader.ReadToEnd(); Console

What is the best way to cleanup the resources used by a Crystal Reports ReportDocument object?

喜欢而已 提交于 2019-12-06 19:14:03
问题 I am working on an application that uses Crystal Reports for the reporting. It opens a given report in a ReportDocument object, does what it needs to do and then closes the report. using (var report = OpenReport(reportSourceInfo)) { // Do stuff with the report report.Close(); } The OpenReport method does some validation of the source file and returns an open ReportDocument object. Testing has shown that this code does what it's meant to do and appears to have no issues. The problem I'm really