diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.json" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.json" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" new file mode 100644 index 0000000000000000000000000000000000000000..b7312fec8ae71742869ec3c1e5ce178923ccdfdc --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" @@ -0,0 +1,120 @@ +# 斐波那契数列 + +C# 的 foreach 可以遍历实现了迭代器接口的数据。以下哪个选项的代码不对? + +## 答案 + +```csharp +int FibNumbers(int n){ + if(n==0) return 0; + if(n==1) return 1; + return FibNumbers(n-1) + FibNumbers(n-2); +} + +foreach (int number in FibNumbers(7)) { + Console.WriteLine($"{number}"); +} +``` + +## 选项 + +### A + +```csharp +int[] fibNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 }; +foreach (int number in fibNumbers) { + Console.WriteLine($"{number}"); +} +``` + +### B + +```csharp +IEnumerable FibNumbers(int n){ + yield return 0; + yield return 1; + + int a = 0; + int b = 1; + for(int i=1;i{ + int n; + FibNumbersEnumerator e; + public FibNumbers(int n){ + this.n = n; + this.e = new FibNumbersEnumerator(this.n); + } + public IEnumerator GetEnumerator(){ + return this.e; + } + IEnumerator IEnumerable.GetEnumerator(){ + return this.GetEnumerator(); + } +} + +class FibNumbersEnumerator : IEnumerator{ + int n; + int a; + int b; + int index; + public FibNumbersEnumerator(int n){ + this.n = n; + this.Reset(); + } + + public int Current{ + get { return this.a; } + } + + object IEnumerator.Current{ + get { return this.Current; } + } + + public bool MoveNext(){ + if(this.index<1){ + this.a = 0; + this.b = 1; + }else{ + int next = this.a+this.b; + this.a = this.b; + this.b = next; + + } + + this.index +=1; + if(this.index==this.n+2){ + return false; + }else{ + return true; + } + } + + public void Reset(){ + this.a = 0; + this.b = 0; + this.index = 0; + } + + public void Dispose(){} +} +``` diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" index 83fa4f4d5fd1f545f64172b044a07814db23104f..dbba38c15cbdc2cc840f5b3c5855e38b3c89a51f 100644 --- "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" +++ "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" @@ -1,2 +1,103 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using System.Collections; + +int[] fibNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 }; +foreach (int number in fibNumbers) { + Console.WriteLine($"{number}"); +} + +IEnumerable FibNumbers(int n){ + yield return 0; + yield return 1; + + int a = 0; + int b = 1; + for(int i=1;i{ + int n; + FibNumbersEnumerator e; + public FibNumbers(int n){ + this.n = n; + this.e = new FibNumbersEnumerator(this.n); + } + public IEnumerator GetEnumerator(){ + return this.e; + } + IEnumerator IEnumerable.GetEnumerator(){ + return this.GetEnumerator(); + } +} + +class FibNumbersEnumerator : IEnumerator{ + int n; + int a; + int b; + int index; + public FibNumbersEnumerator(int n){ + this.n = n; + this.Reset(); + } + + public int Current{ + get { return this.a; } + } + + object IEnumerator.Current{ + get { return this.Current; } + } + + public bool MoveNext(){ + if(this.index<1){ + this.a = 0; + this.b = 1; + }else{ + int next = this.a+this.b; + this.a = this.b; + this.b = next; + + } + + this.index +=1; + if(this.index==this.n+2){ + return false; + }else{ + return true; + } + } + + public void Reset(){ + this.a = 0; + this.b = 0; + this.index = 0; + } + + public void Dispose(){} +} + + + +int FibNumbers(int n){ + if(n==0) return 0; + if(n==1) return 1; + return FibNumbers(n-1) + FibNumbers(n-2); +} + +foreach (int number in FibNumbers(7)) { + Console.WriteLine($"{number}"); +} \ No newline at end of file