idisposable

Should Dispose() or Finalize() be used to delete temporary files?

南笙酒味 提交于 2019-12-20 10:27:13
问题 I have a class that makes use of temporary files ( Path.GetTempFileName() ) while it is active. I want to make sure these files do not remain on the user's hard drive taking up space after my program is closed. Right now my class has a Close() method which checks if any temporary files used by the class still exist and deletes them. Would it make more sense to put this code in the Dispose() or Finalize() methods instead? 回答1: Better yet would be to create the file with FileOptions

Class that implements IDisposable without Dispose function?

◇◆丶佛笑我妖孽 提交于 2019-12-20 07:26:20
问题 I just pulled in WebSocketSharp via nuget. It's class WebSocket implements IDisposable but doesn't seem to have a Dispose method. How is that possible? I thought if you implement an interface you also have to implement all of it's properties/methods. 回答1: On GitHub in source: #region Explicit Interface Implementations /// <summary> /// Closes the WebSocket connection, and releases all associated resources. /// </summary> /// <remarks> /// This method closes the connection with <see cref=

Nesting 'IDisposable's in a single 'using' statement

不打扰是莪最后的温柔 提交于 2019-12-19 18:22:25
问题 Quick question about using nested disposables in a single 'using' statement: Should I write out each disposable's using statement, or can I nest them into one? Example: using( FileStream inFile = new FileStream( "myFile.txt", FileMode.Open ) ) using( GZipStream gzip = new GZipStream( inFile, CompressionMode.Decompress ) ) using( FileStream outFile = new FileStream( "myNewFile.txt", FileMode.CreateNew ) ) { gzip.CopyTo( outstream ); } vs. using( GZipStream gzip = new GZipStream( new FileStream

Passing an IDisposable object by reference causes an error?

丶灬走出姿态 提交于 2019-12-19 10:16:09
问题 I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject() To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference. But I am getting a compilation error that says The 'ref' argument type doesn't match parameter type In the below (simplified) code, both _Baz and _Bar implement IDisposable. So the questions are, Why am I getting this error? Is there a way to get around it? [UPDATE]

Should I implement IDisposable on a singleton?

余生颓废 提交于 2019-12-19 09:58:24
问题 I have a windows service, which contains a singleton which in turn uses some loggers, message queue listeners and so on. Those classes implements IDisposable . Should I implement IDisposable in singleton itself or do something else to ensure that after service stop/crashing everything will be okay with native resources? The singleton is implemented like this: public class Temp { private static readonly Lazy<Temp> instance = new Lazy<Temp>(() => new Temp()); private Temp() { // create

What's the purpose of implementing the IDisposable interface?

我们两清 提交于 2019-12-19 09:04:03
问题 What's the purpose of implementing the IDisposable interface? I've seen some classes implementing it and I don't understand why. 回答1: If your class creates unmanaged resources, then you can implement IDisposable so that these resources will be cleaned up properly when the object is disposed of. You override Dispose and release them there. 回答2: When your classes makes use of some system resource, it's the class' responsibility to make sure the resource is freed too. By .Net design you're

Does the using statement dispose only the first variable it create?

别来无恙 提交于 2019-12-19 06:57:24
问题 Let's say I have a disposable object MyDisposable whom take as a constructor parameter another disposable object. using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable())) { //whatever } Assuming myDisposable don't dispose the AnotherDisposable inside it's dispose method. Does this only dispose correctly myDisposable ? or it dispose the AnotherDisposable too? 回答1: using is an equivalent of MyDisposable myDisposable = new MyDisposable(new AnotherDisposable()); try { /

Does the using statement dispose only the first variable it create?

对着背影说爱祢 提交于 2019-12-19 06:57:13
问题 Let's say I have a disposable object MyDisposable whom take as a constructor parameter another disposable object. using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable())) { //whatever } Assuming myDisposable don't dispose the AnotherDisposable inside it's dispose method. Does this only dispose correctly myDisposable ? or it dispose the AnotherDisposable too? 回答1: using is an equivalent of MyDisposable myDisposable = new MyDisposable(new AnotherDisposable()); try { /

Calling Environment.Exit() Within a Using Block

只愿长相守 提交于 2019-12-19 05:23:18
问题 If I have a console application with code like: using (DisposableObject object = new DisposableObject()) { if (a condition) Environment.Exit(0); // Do Stuff } Will my object be properly disposed? Or does the thread die before the object is cleaned up? 回答1: Your application will terminate and all managed memory will be released at that point. The generated finally block will not execute, so any Dispose methods will not be called, so any non-managed resources may very well not be released. See

Why do HttpClient.PostAsync and PutAsync dispose the content?

。_饼干妹妹 提交于 2019-12-19 03:21:53
问题 The behavior of the HttpClient.PostAsync method is to dispose of the provided HttpContent object. There are many ways to get around this behavior including constructing a new HttpContent for each call made on the client or loading the content to a stream and changing the pointer. I'm wondering why invoking this method automatically invokes the disposal of its IDisposable parameters? As far as I'm aware this is not a common behavior in .NET It's also worth noting that this behavior is observed