BuildingCoder.CmdCollectorPerformance.IterateOverCollector C# (CSharp) Method

IterateOverCollector() private method

Iterate directly over the filtered element collector. In general, there is no need to create a copy of it. Calling ToElements creates a copy, allocating space for that and wasting both memory and time. No need to cast either, foreach can do that automatically.
private IterateOverCollector ( Document doc ) : IEnumerable
doc Document
return IEnumerable
        IEnumerable<Element> IterateOverCollector(
            Document doc)
        {
            // Do not do this!

              FilteredElementCollector collector
            = new FilteredElementCollector( doc );

              collector.OfClass( typeof( Family ) ).ToElements();

              IEnumerable<Family> nestedFamilies
            = collector.Cast<Family>();

              String str = "";

              foreach( Family f in nestedFamilies )
              {
            str = str + f.Name + "\n";

            foreach( ElementId symbolId in
              f.GetFamilySymbolIds() )
            {
              Element symbolElem = doc.GetElement(
            symbolId );

              str = str + " family type: "
            + symbolElem.Name + "\n";
            }
              }

              // Iterate directly over the collector instead.
              // No need for ToElements, which creates a copy.
              // The copy wastes memory and time.
              // No need for a cast, even.

              FilteredElementCollector families
            = new FilteredElementCollector( doc )
              .OfClass( typeof( Family ) );

              foreach( Family f in families )
              {
            str = str + f.Name + "\n";

            // ...
              }
              return families;
        }