idisposable

Am I implementing IDisposable correctly?

不打扰是莪最后的温柔 提交于 2019-12-17 15:39:11
问题 This class uses a StreamWriter and therefore implements IDisposable . public class Foo : IDisposable { private StreamWriter _Writer; public Foo (String path) { // here happens something along the lines of: FileStream fileWrite = File.Open (path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); _Writer = new StreamWriter (fileWrite, new ASCIIEncoding ()); } public void Dispose () { Dispose (true); GC.SuppressFinalize (this); } ~Foo() { Dispose (false); } protected virtual void

Does foreach automatically call Dispose?

强颜欢笑 提交于 2019-12-17 07:23:08
问题 In C#, Does foreach automatically call Dispose on any object implementing IDisposable? http://msdn.microsoft.com/en-us/library/aa664754(v=vs.71).aspx seems to indicate that it does: *Otherwise, the collection expression is of a type that implements System.IEnumerable, and the expansion of the foreach statement is: Copy IEnumerator enumerator = ((System.Collections.IEnumerable)(collection)).GetEnumerator(); try { while (enumerator.MoveNext()) { ElementType element = (ElementType)enumerator

Questions about Entity Framework Context Lifetime

扶醉桌前 提交于 2019-12-17 06:38:09
问题 I have some questions about the desired lifetime of an Entity Framework context in an ASP.NET MVC application. Isn't it best to keep the context alive for the shortest time possible? Consider the following controller action: public ActionResult Index() { IEnumerable<MyTable> model; using (var context = new MyEntities()) { model = context.MyTable; } return View(model); } The code above won't work because the Entity Framework context has gone out of scope while the view renders the page. How

returning in the middle of a using block

会有一股神秘感。 提交于 2019-12-17 04:54:49
问题 Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? 回答1: As several others have pointed out in general this is not a problem. The only case it will cause you issues is if you return in the middle of a using statement and additionally return the in using variable. But then again, this would also cause you issues even if you didn't return and simply kept a reference to a

Will the Garbage Collector call IDisposable.Dispose for me?

痴心易碎 提交于 2019-12-16 21:18:52
问题 The .NET IDisposable Pattern implies that if you write a finalizer, and implement IDisposable, that your finalizer needs to explicitly call Dispose. This is logical, and is what I've always done in the rare situations where a finalizer is warranted. However, what happens if I just do this: class Foo : IDisposable { public void Dispose(){ CloseSomeHandle(); } } and don't implement a finalizer, or anything. Will the framework call the Dispose method for me? Yes I realise this sounds dumb, and

Using 'as IDisposable' in a using block

风格不统一 提交于 2019-12-14 04:18:18
问题 Edit: My question isn't about a using block and how it works. My question is about the difference in the two ways to do it, shown below. I'm reading the CQRS Journey Guide, and I don't understand this line of code: using (repo as IDisposable) What does that mean? Why use it as IDisposable? In a typical using block, one doesn't need to use it as IDisposable: using (var repo = this.respositoryFactory()) { // ... } Any idea why the authors wrote it the first way instead of the second way? This

Return a Disposable object for use in a using block

旧城冷巷雨未停 提交于 2019-12-14 03:57:25
问题 How do I return a disposable object in my function to ensure that it works properly within a using block? In my function, I want to act on the disposable object and also account for errors, which complicates this. I have something similar to the following code so far: DBHandle GetDB() { /* // I can't do this, because the using block would dispose of my object within this function using( var db = DatabaseObj.GetHandle() ) { db.Open(); return db; } */ var db = DatabaseObj.GetHandle(); try { db

Closing a file without using using

让人想犯罪 __ 提交于 2019-12-14 03:49:56
问题 I have a class which reads data from one file stream and writes to another. I am concerned about closing the streams after the processing has finished in closeFiles(). How would you handle the possibility that the dispose of one stream may throw an exception stopping the dispose of the other stream from being called.? Should I be calling close and dispose on the streams or just one? What happens if I catch any errors from the stream disposes and then continue with moving and deleting of the

Form.ShowDialog() and dispose

守給你的承諾、 提交于 2019-12-14 03:43:33
问题 If I have a method like this: public void Show() { Form1 f = new Form1(); f.ShowDialog(); } Do I still need to call dispose on the form even though it will go out of scope, which will be eligible for garbage collection. From some testing, calling this Show() multiple times .. at some point it seems like the GC collects it since I can see the memory spiking then it goes down at some point in time. From MSDN it seems to say you MUST call dispose when the form is not needed anymore. 回答1: In your

How do I lock access to a file when a user has it open?

为君一笑 提交于 2019-12-14 02:07:31
问题 I'm writing a C#.NET program which uses XmlSerializer to serialize and deserialize the project the current user is working on to and from an XML file. This is working fine, but I'm trying to figure out a way to prevent two users from opening the same file off a network drive and having one user overwrite the previous user's save. I essentially want the behavior that MS Word has, where if the program can not gain write access to a file when it is being opened it opens the file in a read only