idisposable

If I return a value inside a using block in a method, does the using dispose of the object before the return?

ぐ巨炮叔叔 提交于 2020-01-19 14:20:12
问题 I'm going through some old C#.NET code in an ASP.NET application making sure that all SqlConnections are wrapped in using blocks. I know that using is the same as try / finally where it disposes of the object in the finally no matter what happens in the try . If I have a method that returns a value inside the using , even though execution leaves the method when it returns, does it still call .Dispose() on my object before/during/after it's returning? public static SqlCommand getSqlCommand

IDisposable and managed resources [duplicate]

泪湿孤枕 提交于 2020-01-15 12:38:06
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Proper use of the IDisposable interface I have a class that has both managed and unmanaged resources. I am using IDisposable to release un-managed resources. Should I release managed resources in the dispose method? Or I can leave it to GC to release managed resources? 回答1: If you have a look at the following documentation you will find the line: Free any disposable resources a type owns in its Dispose method.

Do 'Intermediate IObservables' without final subscribers get kept in memory for the lifetime of the root IObservable

与世无争的帅哥 提交于 2020-01-14 09:59:10
问题 For example, consider this: public IDisposable Subscribe<T>(IObserver<T> observer) { return eventStream.Where(e => e is T).Cast<T>().Subscribe(observer); } The eventStream is a long lived source of events. A short lived client will use this method to subscribe for some period of time, and then unsubscribe by calling Dispose on the returned IDisposable . However, while the eventStream still exists and should be kept in memory, there has been 2 new IObservables created by this method - the one

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

﹥>﹥吖頭↗ 提交于 2020-01-13 19:59:27
问题 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

using(IDisposable obj = new …) in C# to write code blocks in stream (e.g. XML)

亡梦爱人 提交于 2020-01-13 09:07:10
问题 I have started to use classes implementing IDisposable to write blocks in streams, with the using statement. This is helpful to keep a correct nesting and avoid missing or wrongly placed start/end parts. Basically, the constructor writes the start of a block (e.g. opening XML tag), Dispose() the end (e.g. closing XML tag). Example is the UsableXmlElement below (it's for large XMLs, so LINQ to XML or XmlDocument in memory are no options). However, these IDisposable do not implement the

Should I bother calling dispose on objects which share lifetime of process?

时光总嘲笑我的痴心妄想 提交于 2020-01-12 16:00:36
问题 I am aware that all objects which implement IDisposable should be disposed of as soon as they are no longer needed in order to free the memory used by their unmanaged resources. My question relates to objects which I know for a fact will live until the host process itself is terminated. Would it make any difference if I dispose of them or not? Is there any chance of memory not being freed when the process dies? What about GDI objects? Would the GDI handles be freed when the process dies even

Should I bother calling dispose on objects which share lifetime of process?

点点圈 提交于 2020-01-12 15:58:09
问题 I am aware that all objects which implement IDisposable should be disposed of as soon as they are no longer needed in order to free the memory used by their unmanaged resources. My question relates to objects which I know for a fact will live until the host process itself is terminated. Would it make any difference if I dispose of them or not? Is there any chance of memory not being freed when the process dies? What about GDI objects? Would the GDI handles be freed when the process dies even

ServiceContainer, IoC, and disposable objects

爷,独闯天下 提交于 2020-01-12 03:35:09
问题 I have a question, and I'm going to tag this subjective since that's what I think it evolves into, more of a discussion. I'm hoping for some good ideas or some thought-provokers. I apologize for the long-winded question but you need to know the context. The question is basically: How do you deal with concrete types in relation to IoC containers? Specifically, who is responsible for disposing them, if they require disposal, and how does that knowledge get propagated out to the calling code? Do

Is it meaningful for AutoCloseable's close method to throw an exception? How should this be handled?

删除回忆录丶 提交于 2020-01-11 10:04:29
问题 In C#, it is considered bad practice to throw exceptions in the Dispose method of an IDisposable . By contrast, in java the close method of AutoCloseable allows any Exception whatsoever to be thrown and forces the caller to handle it somehow. But what is the caller reasonably expected to do if this happens? This suggests that the attempt to close the resource failed somehow. So does the user have to try to close the resource again before continuing, perhaps with some sort of exponential

Should I Treat Entity Framework as an Unmanaged Resource?

大憨熊 提交于 2020-01-11 08:29:05
问题 I am working with a class that uses a reference to EF in its constructor. I have implemented IDisposable , but I'm not sure if I need a destructor because I'm not certain I can classify EF as an unmanaged resource. If EF is a managed resource, then I don't need a destructor, so I think this is a proper example: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //... Db = new Entities(connectionStringName); } private bool _isDisposed; public