Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If my struct implements IDisposable will it be boxed when used in a using statement?

Tags:

c#

If my struct implements IDisposable will it be boxed when used in a using statement?

Thanks

Edit: this timedlock is a struct and implements Idisposable. http://www.interact-sw.co.uk/iangblog/2004/04/26/yetmoretimedlocking

Edit 2: Looking at the IL it seems If your struct exposes Dispose() as public, the compiler calls Dispose when an instance of the struct goes out of scope if you forget to call Dispose() (for example, you are not using the "using" statement)?

like image 612
john green Avatar asked Mar 09 '10 22:03

john green


People also ask

Is IDisposable called automatically?

Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.

When should a class implement IDisposable?

in a class, you should implement IDisposable and overwrite the Dispose method to allow you to control when the memory is freed. If not, this responsibility is left to the garbage collector to free the memory when the object containing the unmanaged resources is finalized.

What is IDisposable interface in C implement the Dispose method?

The Dispose method is automatically called when a using statement is used. All the objects that can implement the IDisposable interface can implement the using statement. You can use the ildasm.exe tool to check how the Dispose method is called internally when you use a using statement.

Where is IDisposable used?

IDisposable is defined in the System namespace. It provides a mechanism for releasing unmanaged resources. When your application or class library encapsulates unmanaged resources such as files, fonts, streams, database connections, etc, they should implement the IDisposable interface or the IAsyncDisposable interface.


2 Answers

Per Eric Lippert:

A call to IDisposable.Dispose on a struct is generated as a constrained virtual call, which most of the time does NOT box the value.

A constrained virtual call on a value type only boxes the value if the virtual method is NOT implemented by the type. The only circumstances under which a virtual method can be unimplemented by the value type is when the method is, say, ToString, and implemented by the base class, System.ValueType.

See section 2.1 of Partition III of the CLI documentation for more detail.

like image 117
Ta01 Avatar answered Sep 21 '22 14:09

Ta01


This is a duplicate of When does a using-statement box its argument, when it's a struct?

UPDATE: This question was the subject of my blog in March of 2011. Thanks for the great question.

A few points:

  • As others have correctly pointed out, a value type that implements IDisposable is not boxed when it is disposed as a consequence of control leaving a using statement.
  • This is technically a violation of the C# specification. The spec states that the finally block should have the semantics of ((IDisposable)resource).Dispose(); which is clearly a boxing conversion. We do not actually generate the boxing conversion. Since most of the time this is what you want anyway, we're not losing any sleep over it.
  • A disposable value type seems like a potentially bad idea. It's too easy to accidentally make a copy of a value type; they are copied by value after all.
  • Why on earth do you care whether this boxes or not? I hope you're not asking this because you want the dispose method to mutate the variable containing the value type. That would be a bad idea indeed. Mutable value types are evil.
like image 44
Eric Lippert Avatar answered Sep 23 '22 14:09

Eric Lippert