In C-Series languages, the For loop plays an important role. It's hard to imagine that there is not a for loop in the 100 line C code ( I have a friend who wrote a thousands of-line algorithm, no use for loop, I was surprised ), like, 100 lines of Chinese inside, no one "." Visible, the For loop is the basic building block of the code. Since the For loop is typically used to manipulate a string of objects of the same type, it can be seen from the side that it is often accompanied by an "array". In the more popular words, "for loop" and "array" are gold partners.
In C #, the Foreach loop is introduced, which is the same as the For loop nature, because in the Foreach loop, the indicator I is omitted (often, it is used only for the first item, there is no other use ), many people gladly accept the Foreach loop, after all, did not take the For loop , it's still there!
Programming language has been evolving, has experienced: ...-assembly language-...-Procedural languages-...-Object-oriented language-...。 Overall, more and more advanced, more and more abstract. Contemporary programmers can program without knowing what the hardware is, call a sort method, and do not know whether to use "bubbling" or "fast" sorting algorithms ( foreigners have helped us!). Whenever I realize the fact that "the gap is more than 20 years", I ..., all right, don't want to grieve! ).
In C # 3.0, the introduction of extension Methods is accompanied by a new gadget, Linq. Using utility Reflector.exe to open the System.Linq namespace in System.Core.dll, there is a enumerable static class, which has a large number of extension methods for "array" operations ( you can think of basic all have, do not believe to see!) ).
For a friend who used a for loop, if he/she stops using it, it will feel like the day is over. Rest assured, I will not persuade him/her to stop using, just like quitting smoking, is his own business. ( another digression, to the next!) )
Below I use code to demonstrate, how to use "extension method/linq" to kill "for loop":
[Test]
public void Oldsum ()
{
int SUM0 = 0;
for (int i = 0; i <; i++)
{
SUM0 + = i;
}
Assert.AreEqual (SUM0);
}
[Test]
public void Newsum ()
{
int sum1 = Enumerable.range (0, 10). Sum ();
int sum2 = Enumerable.range (0, 10). Aggregate ((x, y) = + x + y);
int sum3 = Enumerable.range (0, 10). Aggregate (0, (x, y) = + x + y);
Assert.AreEqual (SUM1);
Assert.AreEqual (SUM2);
Assert.AreEqual (SUM3);
}
Note: whether to sum or calculate a string of numbers, in the final analysis, are a bunch of things into a thing , at this time with Aggregate!
[Test]
public void Oldfilter ()
{
int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> odd_list = new list<int> ();
for (int i = 0; i < arr. Length; i++)
{
If (Arr[i]% 2 = = 1)
{
Odd_list. ADD (Arr[i]);
}
}
int[] Odd_arr = odd_list. ToArray ();
Assert.that (Odd_arr, Is.equivalentto (new int[] {1, 3, 5, 7, 9}));
}
[Test]
public void Newfilter ()
{
int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] Odd_arr = arr. Where (x = = x% 2 = = 1). ToArray ();
Assert.that (Odd_arr, Is.equivalentto (new int[] {1, 3, 5, 7, 9}));
}
Note: Whether it is to take odd or even, in the final analysis, is to take a string of things in some things , this time with Where!
[Test]
public void Oldmap ()
{
int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> new_list = new list<int> ();
for (int i = 0; i < arr. Length; i++)
{
New_list. ADD (Arr[i] * 10);
}
int[] New_arr = new_list. ToArray ();
Assert.that (New_arr, Is.equivalentto (new int[] {0, 10, 20, 30, 40, 50, 60, 70, 80, 90}));
}
[Test]
public void Newmap ()
{
int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] New_arr = arr. Select (x = x * 10). ToArray ();
Assert.that (New_arr, Is.equivalentto (new int[] {0, 10, 20, 30, 40, 50, 60, 70, 80, 90}));
}
Note: Whether it is X10 or +99, in the final analysis, is to turn a string of things into a bunch of new things , this time with a Select!
[Test]
public void Printmultiplicationfact ()
{
Console.Write (
"1 x 1= 1 \ n"
+ "1 x 2= 2 2 x 2= 4 \ n"
+ "1 x 3= 3 2 x 3= 6 3 x 3= 9 \ n"
+ "1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16 \ n"
+ "1 x 5= 5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25 \ n"
+ "1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36 \ n"
+ "1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=49 \ n"
+ "1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=64 \ n"
+ "1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81 \ n"
);
/********************* method One: Nested loops *************************/
for (int j = 1; J <; J + +)
{
for (int i = 1; i <; i++)
{
if (i <= j)
{
Console.Write ("{0, 2} x{1, 2}={2, 2}\t", I, J, I * j);
}
}
Console.Write ("\ n");
}
/********************* method Two: extension method *************************/
Enumerable.range (1, 9)
. SelectMany (j = enumerable.range (1, 9), (j, i) = = new {i, J})
. Where (x = x.i <= x.j)
. GroupBy (x = X.J)
. Select (g = G.aggregate ("", (A, X) = A + string. Format ("{0, 2} x{1, 2}={2, 2}\t", x.i, X.J, x.i * x.j)))
. ToList (). ForEach (x = Console.WriteLine (x));
/********************* method Three: LINQ expression ************************/
(
From J in Enumerable.range (1, 9)
From I in Enumerable.range (1, 9)
where I <= J
Group New {i, J} by J into G
Select New
{
Lineno = G.key,
line = G.aggregate ("", (A, X) = + A + string. Format ("{0, 2} x{1, 2}={2, 2}\t", x.i, X.J, x.i * x.j))
}
). ToList (). ForEach (g = Console.WriteLine (G.line));
}
Note: for nested for loops , use SelectMany!
Disclaimer: The For loop is good and you can continue to use it if you want. If you like to try something new, I want to tell you: "Maybe you can try it !" "
Appendix 1: Multiplication Formulas
1 x 1 = 1
1 x 2= 2 2 x 2 = 4
1 x 3= 3 2 x 3= 6 3 x 3 = 9
1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16
1 x 5= 5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25
1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36
1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=49
1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=64
1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81
Appendix 2: Complete code
Using system;using system.collections.generic;using system.linq;using system.text;using NUnit.Framework;namespace ksharp{[testfixture] public class Testforloop {[Test] public void Oldsum () {in T SUM0 = 0; for (int i = 0; i < i++) {SUM0 + = i; } assert.areequal (SUM0); } [Test] public void newsum () {int sum1 = enumerable.range (0, 10). Sum (); int sum2 = Enumerable.range (0, 10). Aggregate ((x, y) = + x + y); int sum3 = Enumerable.range (0, 10). Aggregate (0, (x, y) = + x + y); Assert.AreEqual (SUM1); Assert.AreEqual (SUM2); Assert.AreEqual (SUM3); } [Test] public void Oldfilter () {int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; list<int> odd_list = new list<int> (); for (int i = 0; i < arr. Length; i++) {if (Arr[i]% 2 = = 1) {odd_list. ADD (Arr[i]); }} int[] Odd_arr = odd_list. ToArray (); Assert.that (Odd_arr, Is.equivalentto (new int[] {1, 3, 5, 7, 9})); } [Test] public void Newfilter () {int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] Odd_arr = arr. Where (x = = x% 2 = = 1). ToArray (); Assert.that (Odd_arr, Is.equivalentto (new int[] {1, 3, 5, 7, 9})); } [Test] public void Oldmap () {int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; list<int> new_list = new list<int> (); for (int i = 0; i < arr. Length; i++) {new_list. ADD (Arr[i] * 10); } int[] New_arr = new_list. ToArray (); Assert.that (New_arr, Is.equivalentto (new int[] {0, 10, 20, 30, 40, 50, 60, 70, 80, 90})); } [Test] public void Newmap () {int[] arr = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] New_arr = arr. Select (x = x * 10). ToArray (); Assert.that (New_arr, Is.equivalentto (new int[] {0, 10, 20, 30, 40, 50, 60, 70, 80, 90})); } [Test] public void Printmultiplicationfact () {Console.Write ("1 x 1 = 1 \ n "+" 1 x 2= 2 2 x 2= 4 \ n "+" 1 x 3= 3 2 x 3= 6 3 x 3= 9 \ n " + "1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16 \ n" + "1 x 5= 5 2 x 5=10 3 X 5=15 4 x 5=20 5 x 5=25 \ n "+" 1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36 \ n "+" 1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7 =49 \ n "+" 1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x8=48 7 x 8=56 8 x 8=64 \ n "+" 1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81 \ n "); /********************* method One: Nested loops *************************/for (int j = 1; J <; J + +) { for (int i = 1; i < i++) {if (I <= j) { Console.Write ("{0, 2} x{1, 2}={2, 2}\t", I, J, I * j); }} console.write ("\ n"); }/********************* Method Two: extension method *************************/Enumerable.range (1, 9) . SelectMany (j = enumerable.range (1, 9), (j, i) = = new {i, J}). Where (x = x.i <= x.j). GroupBy (x = X.J). Select (g = G.aggregate ("", (A, X) = A + string. Format ("{0, 2} x{1, 2}={2, 2}\t", X.I, X.J, X.i * x.j)). ToList (). ForEach (x = Console.WriteLine (x)); /********************* method Three: LINQ Expression ************************/(from J in Enumerable.range (1, 9) From I in Enumerable.range (1, 9) where I <= J group New {I, J} by J into G Select New {Lineno = G.key, line = G.aggregate ("", (A, X) = + A + string. Format ("{0, 2} x{1, 2}={2, 2}\t", x.i, X.J, x.i * x.j))}). ToList (). ForEach (g = Console.WriteLine (G.line)); } }
LINQ kills for Loop