Amit Padhiyar

Amit Padhiyar

  • 1.3k
  • 448
  • 6.5k

How do i reduce loop size using list or linq in C#

Jul 25 2019 4:51 AM
First, I used 1000 time loop for drawing lines
 
int x = 0; 
for(int i=0;i<999;i++){
     Line l = new Line();
     l.X1 = x;
     l.X2 = x;
     l.Y1 = 0;
     l.Y2 = 50;
     Somthing.Children().Add(l);
 
     x+=15; 
}
Then i got second idea.. 
 Loop size will reduce by 1000/5 = 200
int x = 0;
for(int i=0;i<199;i++){
Line l1 = new Line();
l1.X1 = x;
l1.X2 = x;
l1.Y1 = 0;
l1.Y2 = 50;
Somthing.Children().Add(l1);
x+=15;
Line l2 = new Line();
l2.X1 = x;
l2.X2 = x;
l2.Y1 = 0;
l2.Y2 = 50;
Somthing.Children().Add(l2);
x+=15;
Line l3 = new Line();
l3.X1 = x;
l3.X2 = x;
l3.Y1 = 0;
l3.Y2 = 50;
Somthing.Children().Add(l3);
x+=15;
Line l4 = new Line();
l4.X1 = x;
l4.X2 = x;
l4.Y1 = 0;
l4.Y2 = 50;
Somthing.Children().Add(l4);
x+=15;
Line l5 = new Line();
l5.X1 = x;
l5.X2 = x;
l5.Y1 = 0;
l5.Y2 = 50;
Somthing.Children().Add(l5);
x+=15;
}
 
Now i need something that can reduce code + fast execution with small loop 

Answers (3)