Author Topic: Collection Initialization  (Read 194 times)

0 Members and 1 Guest are viewing this topic.

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1438
  • class keyThumper<T>:ILazy<T>
Collection Initialization
« on: April 24, 2022, 08:11:52 PM »
Just a reminder regarding 'Collection Initialization'

Code - C#: [Select]
  1.                         DBObjectCollection acDBObjColl = new DBObjectCollection();
  2.                         acDBObjColl.Add(acLine1);
  3.                         acDBObjColl.Add(acLine2);
  4.                         acDBObjColl.Add(acCirc2);
  5.                         acDBObjColl.Add(acMText4);
  6.  

Since c#6 Can become
Code - C#: [Select]
  1.                         DBObjectCollection acDBObjColl = new DBObjectCollection
  2.                         {
  3.                             acLine1,
  4.                             acLine2,
  5.                             acCirc2,
  6.                             acMtext4
  7.                         };
  8.  


We can also use it when constructing in a using statement :
but I think it could lead to confusion, so I won't use it.
Code - C#: [Select]
  1.     // Create a circle that is at 2,3 with a radius of 4.25
  2.     using (Circle acCirc = new Circle
  3.     {
  4.         Center = new Point3d(2, 3, 0),
  5.         Radius = 4.25
  6.     })
  7.     {
  8.         // Add the new object to the block table record and the transaction
  9.         acModelSpaceRec.AppendEntity(acCirc);
  10.         acTrans.AddNewlyCreatedDBObject(acCirc, true);
  11.  
  12. < . . . . >
  13.  

This compiles to the following. It's interesting how the compiler shuffles the (Decompiled/Reflected) code

Code - C#: [Select]
  1. [CommandMethod("CopySingle")]
  2. public static void CopySingle()
  3. {
  4.     Document document = Application.get_DocumentManager().get_MdiActiveDocument();
  5.     Database database = document.get_Database();
  6.     using (Transaction transaction = database.get_TransactionManager().StartTransaction())
  7.     {
  8.         BlockTableRecord record = transaction.GetObject((transaction.GetObject(database.get_BlockTableId(), 0)
  9.                                                             as BlockTable).get_Item(BlockTableRecord.ModelSpace), 1)
  10.                                                                  as BlockTableRecord;
  11.         Circle circle1 = new Circle();
  12.         circle1.set_Center(new Point3d(2.0, 3.0, 0.0));
  13.         circle1.set_Radius(4.25);
  14.         using (Circle circle = circle1)
  15.         {
  16.             record.AppendEntity(circle);
  17.             transaction.AddNewlyCreatedDBObject(circle, true);
  18.             Circle circle2 = circle.Clone() as Circle;
  19.             circle2.set_Radius(1.0);
  20.             record.AppendEntity(circle2);
  21.             transaction.AddNewlyCreatedDBObject(circle2, true);
  22.         }
  23.        
  24.         transaction.Commit();
  25.     }
  26. }
  27.  
  28.  



Coming back to this stuff I'm reminded of the axiom "use it or lose it"
... the wet grey stuff doesn't keep knowledge we don't use and reinforce  regularly.
« Last Edit: April 25, 2022, 04:46:25 PM by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

Atook

  • Swamp Rat
  • Posts: 994
  • AKA Tim
Re: Collection Initialization
« Reply #1 on: April 25, 2022, 12:56:37 PM »
Thanks kdub, a good reminder for me as well.