idisposable

Yield return from a try/catch block [duplicate]

帅比萌擦擦* 提交于 2019-12-19 02:37:08
问题 This question already has answers here : yield return with try catch, how can i solve it (10 answers) Closed 6 years ago . As Eric Lippert described in this article, yield return is not allowed within try/catch clauses. Is there a nice way I could get something like this, without having to write my own IEnumerator by hand: public IEnumerable<Data> GetData() { var transaction = Session.BeginTransaction()); try { IQuery q = CreateQuery(session); foreach (var result in q.Enumerable()) yield

How to properly dispose of a WebResponse instance?

南楼画角 提交于 2019-12-18 18:41:38
问题 Normally, one writes code something like this to download some data using a WebRequest. using(WebResponse resp = request.GetResponse()) // WebRequest request... using(Stream str = resp.GetResponseStream()) ; // do something with the stream str Now if a WebException is thrown, the WebException has a reference to the WebResponse object, which may or may not have Dispose called (depending on where the exception has happened, or how the response class is implemented) - I don't know. My question

Will ignoring IDisposable cause memory leaks?

亡梦爱人 提交于 2019-12-18 14:57:23
问题 In the comments to an answer I wrote we had a discussion about memory leaks and IDisposable where we didn't come to any real conclusion. A class that handles unmanaged resources likely implements IDisposable . If ignore that and neither call Dispose nor wraps the object in a using - will that lead to the unmanaged resource being leaked? Or will it be properly cleaned up when the GC collects the object? We can assume that the class handling the unmanaged resource has a correct implementation

What should be passed as the objectName when throwing an ObjectDisposedException?

ⅰ亾dé卋堺 提交于 2019-12-18 13:51:36
问题 When implementing IDisposable, I undertand that every method that shouldn't be called after the object's been disposed should throw the ObjectDisposedException . But what is the standard for the name object that should be passed to the exception's constructor? 回答1: I believe the recommended practice is to throw the following: throw new ObjectDisposedException(GetType().FullName); Or including the check, these two lines of code at the top of each method that needs it (obviously not the Dispose

Why should Dispose() be non-virtual?

我怕爱的太早我们不能终老 提交于 2019-12-18 12:06:07
问题 I'm new to C#, so apologies if this is an obvious question. In the MSDN Dispose example, the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own method. Thanks! 回答1: Typical usage is that Dispose() is overloaded, with a public, non-virtual Dispose() method, and a virtual, protected Dispose(bool). The

How to implement IDisposable properly

旧巷老猫 提交于 2019-12-18 12:00:48
问题 I've seen so much C# code in my time as a developer that attempt to help the GC along by setting variables to null or calling Dispose() on classes (DataSet for example) within thier own classes Dispose() method that I've been wondering if there's any need to implement it in a managed environment. Is this code a waste of time in its design pattern? class MyClass : IDisposable { #region IDisposable Members public void Dispose() { otherVariable = null; if (dataSet != null) { dataSet.Dispose(); }

How to implement IDisposable properly

房东的猫 提交于 2019-12-18 12:00:20
问题 I've seen so much C# code in my time as a developer that attempt to help the GC along by setting variables to null or calling Dispose() on classes (DataSet for example) within thier own classes Dispose() method that I've been wondering if there's any need to implement it in a managed environment. Is this code a waste of time in its design pattern? class MyClass : IDisposable { #region IDisposable Members public void Dispose() { otherVariable = null; if (dataSet != null) { dataSet.Dispose(); }

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

送分小仙女□ 提交于 2019-12-18 06:14:48
问题 Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! 回答1: The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where Create(0) returns null (i.e. it doesn't attempt to call Dispose() on a null instance). 回答2: A using statement is always better

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

落花浮王杯 提交于 2019-12-18 06:13:03
问题 Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! 回答1: The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where Create(0) returns null (i.e. it doesn't attempt to call Dispose() on a null instance). 回答2: A using statement is always better

How to dispose objects having asynchronous methods called?

梦想与她 提交于 2019-12-18 05:43:32
问题 I have this object PreloadClient which implements IDisposable , I want to dispose it, but after the asynchronous methods finish their call... which is not happening private void Preload(SlideHandler slide) { using(PreloadClient client = new PreloadClient()) { client.PreloadCompleted += client_PreloadCompleted; client.Preload(slide); } // Here client is disposed immediately } private void client_PreloadCompleted(object sender, SlidePreloadCompletedEventArgs e) { // this is method is called