In general, I have a program on which there are two rectangle , when we mouse_down ( mouse_down ) the first rectangle we fix the coordinates of the mouse, while moving ( mouse_move ) we update the coordinates of the rectangle and start using FindVisualChildren<Rectangle>(childUI) and FillContainsWithDetail to check cyclically that there is an intersection if the intersection catches something from the second (on which it is superimposed) we change the Rectangle color, and if we release it at the intersection, we apply the first rectangle to the second and change the color of the first.
So, if I have 200 rectangles then the program will crash because of my cycle, tell me how you could optimize all this?
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } } 