Use a Using block with Transactions

The code I originally had below in the Duplicate Mark sample works fine when everything goes according to plan. But a more robust approach would use a “using” block as shown below.

using( Transaction transaction = new Transaction(document,"action") )
{
   transaction.Start();
  // ....
   transaction.Commit();
}

This goes for every transaction as well as every transaction group and sub-transaction. The objective is to make sure those objects are properly closed (particularly in case of an exception). More on the subject is in the MSDN C# reference.

One thought on “Use a Using block with Transactions

Leave a reply to Revit API : "using" Declarations / Statements (C#) | Got BIM?Got BIM? Cancel reply