Is it valid to use properties of Disposing object

1.2k views Asked by At

can anybody tell me if it is valid to use the properties of Disposing object ? for e.g. in the following code DataTable is getting Dispose but its property DefaultView is used later,

public DataView MyView { get; set; }

    private void button2_Click(object sender, EventArgs e)
    {

        using (DataTable table = new DataTable())
        {
            using (DataColumn dc = new DataColumn("s"))
            {
                table.Columns.Add(dc);
                MyView = table.DefaultView;

            }
            Debug.Write(table.Columns[0].ColumnName);
        }
    }

I don't get any error if i use MyView. but isn't it true that Disposing object dispose all its properties in this case DefaultView.

3

There are 3 answers

0
Peter Duniho On BEST ANSWER

It depends on the type, but as a general rule you should only call Dispose() when you are completed done using the object. Most IDisposable implementations will throw an ObjectDisposedException if you try to access any member after disposing the object.

There's nothing inherent about IDisposable that forces an implementing class to invalidate all access to all members upon disposal.

Only if the type specifically documents that it allows you to call certain members after it's been disposed should you consider doing so. And personally, I'd try to stay away from using types that are implemented that way.

Note that this pertains to the object itself, not necessarily objects it references. That is, just because object A has a property that references some other object B, that doesn't mean that object B becomes invalid to use when you dispose A.

A good example of this would be the NetworkStream class, which has a Socket property. If you initialize the NetworkStream object as not owning the Socket instance passed to its constructor, then the Socket instance remains valid for use after disposing the NetworkStream, and this is perfectly fine.

4
James Ralston On

The IDisposable interface is implemented when you have unmanaged resources that could cause problems if an exception is raised before those resources are freed.

When used in a using block the compiler generates

try{... } 
catch{... } // may be empty, I can't recall at the moment 
finally 
{
   Dispose() ;
} 

since the value you added was not an unmanaged resource there is no reason to assume it was disposed of. There is still a valid reference to the object, so the GC will not clear the memory. Thinking about it, it would be far scarier if an exception was raised.

C# has memory management under the hood, so it's out of sight out of mind for many programmers. There are certainly some things that are implemented poorly out in the wild since resource management is not something you often think about in c#

0
John Saunders On

The base DataTable and DataColumn classes do implement IDisposable, but their implementations don't do anything. These implement IDisposable because a derived class may need to dispose of resources.

For instance, back in the day, I had a generic, strongly-typed DataTable class that I wrote. It did actually encapsulate the database connection, or possibly a stream. In that case, Dispose really did something.