Returning Inside Using Statements (AutoCAD .Net API)

_config.yml

While writing the above AutoCAD plug-in, I faced a small conundrum in the below code:

  • The question for you is: will the transaction be disposed of, and committed given I have returned the bool before it reaches the end of the using statement?

Will it be disposed?

The short answer is yes. The transaction object implements IDisposable. So, finally you can trust that the transaction will be disposed, and that any objects that it opens will also be similarly disposed.

Will the transaction be committed?

I had a peak into the Dispose methods using Reflector. I didn’t see any automatic committing of any transactions – so I am venturing to say that no, the transaction will not be committed. In other words, we’ll have to restructure the above code to ensure that the transaction is committed before we issue the returning statement. Perhaps the AutoDesk team should abort a transaction if it is not committed before a transaction is disposed?

Summary

So the lesson is: (i) always be sure your transaction is being committed, and (ii) a using statement obviates the need to ensure that your transaction is actually being disposed of.

Written on October 4, 2017