Converting foreach touch into for.

Hi.
I read that the “foreach” can cause lag on phones

foreach (Touch touch in Input.touches) 

This method should work better(forgot why)

for (int i = 0 ; i < firstArray.lenght ; i++)

firstArray*.something = something else//modify…*
I’ve been trying to convert this code that handles touch, into using the for statement instead of foreach but without success
Here’s the code that I need to change:
if(Input.touchCount > 0)
{

  • foreach (Touch touch in Input.touches)*

  • {*

  •  Vector3 inputGuiPosition = touch.position;*
    
  •  inputGuiPosition.y = Screen.height - inputGuiPosition.y;*
    
  •  if(inputGuiPosition.x > Screen.width / 5)*
    
  •  {*
    
  •  	_jump = true;*
    
  •  }*
    
  • }*
    }

for (int i = 0 ; i < Input.touches.Length; i++) {
Vector3 inputGuiPosition = Input.touches*.position;*
// …
}
“This method should work better(forgot why)”
1. The indexed for loop has minor performance advantages under some circumstances.(irrelevant, disregard)
2. Inside of the loop you have the counter “i” so we know the current index. (essential for some algorithms)
3. Can be used to iterate/count arbitrarily. (foreach can only process IEnumerable)
I find the simplified syntax makes foreach a better choice for iterating over an collection.