How to use ForwardIterator?

How to use ForwardIterator?

V__K
Contributor Contributor
328 Views
3 Replies
Message 1 of 4

How to use ForwardIterator?

V__K
Contributor
Contributor

Can you please help me to understand how ForwardIterator() works and how it can be used practically? A basic and simple example would be very helpful to understand the functioning. Is it something that avoids the use of foreach or for loops and makes the process more efficient? I mean I want to know the purpose and significance of the ForwardIterator.
You can also share a reference, if there's any article or blog for the same.

Thanks!

0 Likes
329 Views
3 Replies
Replies (3)
Message 2 of 4

Mohamed_Arshad
Advisor
Advisor

Hi @V__K 

 

   In the Revit API, the ForwardIterator() is used to iterate over elements in a collection sequentially from the beginning to the end. It’s typically used with collections that don’t support direct access by index, allowing you to go through each element one by one in forward order. Kindly check the below code snippet for reference.

 

Reference Code Snippet

 

ParameterSetIterator parameterSet = wall.Parameters.ForwardIterator();

while (parameterSet.MoveNext())
{
    Parameter param = (Parameter)parameterSet.Current;
}

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 4

V__K
Contributor
Contributor

@Mohamed_Arshad 
Yeah, I've seen similar examples.
Can you explain how is it different from foreach loop or what's benefit of the forward iterator compared to the foreach loop? I mean what's special about it.

@naveen.kumar.t 
Would you like to comment on this?

0 Likes
Message 4 of 4

Mohamed_Arshad
Advisor
Advisor

Hi @V__K 

 

ForwardIterator() is quite faster than foreach loop. How it is faster? 

foreach abstracts away the control over iteration, meaning you can’t directly control or optimize the traversal method. For instance, ForwardIterator() in the Revit API is optimized for accessing Revit’s collections directly, minimizing any additional abstraction. In simple we can say the special feature is you have direct control over the iteration.

 

Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes