idisposable

What does Using(.....){...} mean

Possible Duplicates: Using the using statment in c# What is the C# Using block and why should I use it? Just wondering what this means? I've seen lots of tutorials online that have the syntax: using (SqlCeCommand cmd2 = new SqlCeCommand("SELECT city FROM cities JOIN states ON states.id=cities.state WHERE states.state='" + r...

How to dispose the resource when it might be used by multiple clients?

I am implementing a pipeline pattern, the resource, say an image object, will go through the pipeline, and i have no idea how many clients are holding the resource, so what's the better way to dispose the resource? ...

Should I have methods which return lists of Disposable instances?

I have a class, instances of which need to be disposed. I also have several classes that produce these instances, either singly or lists of them. Should I return IList<MyClass> from my methods or should I create a class that is MyClassCollection which is also disposable and return this instead? EDIT: My main reason for asking is tha...

Is there a FxCop rule for local used IDisposable's?

... if I use an IDisposable in a local variable, but do not call Dispose() or use the using() pattern. public void BadMethod() { var fs = new FileStream("file.txt", FileMode.Create); fs.WriteByte(0x55); // no dispose, no using() } Just like the "Types that own disposable fields should be disposable" rule for fields. EDIT...

C++ Ref class not a member of System::IDisposable; trouble implementing IDisposable.

I want to make a global vector of my own object class called "Person". However, the compiler says that error C2039: '{dtor}' : is not a member of 'System::IDisposable' 1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::IDisposable' So I looked up how to implement IDisposable (which I...

Implementing Disposable pattern correctly - auto-implemented properties

One of the rules for implementing Dispose method says: "Throw an ObjectDisposedException from instance methods on this type (other than Dispose) when resources are already disposed. This rule does not apply to the Dispose method because it should be callable multiple times without throwing an exception." See: http://msdn.microsoft.com/...

Can I "inline" a variable if it's IDisposible?

Do I have to do this to ensure the MemoryStream is disposed of properly? using (MemoryStream stream = new MemoryStream(bytes)) using (XmlReader reader = XmlReader.Create(stream)) { return new XmlDocument().Load(reader); } or is it OK to inline the MemoryStream so that it simply goes out of scope? Like this? using (XmlR...

IDisposable, ObjectDisposedException, and threadsafe types

Is there any point in keeping track of the classic bool disposed field on an otherwise threadsafe type for the purposes of conditionally throwing an ObjectDisposedException at the beginning of all primary exposed methods? I've seen this pattern recommended in a few places online but I'm not sure if the authors are using it correctly, ...

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHor...

Using `using(...)` effectively useless and/or inefficient???

Does the following code render the using(...) function/purpose irrelevant? Would it cause a deficiency in GC performance? class Program { static Dictionary<string , DisposableClass> Disposables { get { if (disposables == null) disposables = new Dictionary<string , DisposableClass>(); ...

What happens when 'return' is called from within a 'using' block?

If I have a method with a using block like this... public IEnumerable<Person> GetPersons() { using (var context = new linqAssignmentsDataContext()) { return context.Persons.Where(p => p.LastName.Contans("dahl")); } } ...that returns the value from within the using block, does the IDispos...

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

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 essen...

If I catch exceptions inside a using statement for a SqlConnection, does the using still handle closing and disposing my connection?

I'm going through some old C#.NET code in an ASP.NET application making sure that all SqlConnections are wrapped in using blocks. This piece of code used to open cn and da and close and dispose them in both the catch block and the end of the method. I added the usings and can't figure out for certain if the using blocks still handle dis...

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

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...

Are IDisposable objects meant to be created and disposed just once?

I'm using a huge binary tree like structure whose nodes may or may not make use of unmanaged resources. Some of these resources may take up a lot of memory, but only a few of them will be in use at a time. The initial state of the tree can be seen as 'dormant'. Whenever a node is accessed, that specific node and its children will 'wake ...

How should I handle exceptions in my Dispose() method?

I'd like to provide a class to manage creation and subsequent deletion of a temporary directory. Ideally, I'd like it to be usable in a using block to ensure that the directory gets deleted again regardless of how we leave the block: static void DoSomethingThatNeedsATemporaryDirectory() { using (var tempDir = new TemporaryDirectory(...

How can TcpClient implement IDisposable and not have a public Dispose method?

Just as the title says: How can TcpClient implement IDisposable and not have a public Dispose method? ...

Flyweight and Factory problem with IDisposable

I seem to be mentally stuck in a Flyweight pattern dilemma. First, let's say I have a disposable type DisposableFiddle and a factory FiddleFactory: public interface DisposableFiddle : IDisposable { // Implements IDisposable } public class FiddleFactory { public DisposableFiddle CreateFiddle(SomethingThatDifferentiatesFiddles s...

IDisposable and COM

Since COM objects hold non-memory resources but don't support IDisposable, I'm trying out some ideas to fake it. What do you think of my first attempt? Public Function ComBlock(ByVal ParamArray comObjects As Object()) As IDisposable For i As Integer = 0 To comObjects.Length - 1 If comObjects(i) Is Nothing Then Throw New Argu...

Disposing objects in the Destructor

I have an object that has a disposable object as a member. public class MyClass { private MyDisposableMember member; public DoSomething { using (member = new MyDisposableMember()) { // Blah... } } } There can be many methods in MyClass, all requiring a using statement. But what...