110,502
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class OneToThree : IEnumerable<int>
{
public class OneToThreeEnumerator : IEnumerator<int>, IEnumerator
{
private int _current = 0;
public int Current
{
get { return _current; }
}
public void Dispose()
{
//这里我们不需要清理
}
object System.Collections.IEnumerator.Current
{
get { return _current; }
}
public bool MoveNext()
{
_current++;
return _current <= 3;
}
public void Reset()
{
_current = 0;
}
}
public IEnumerator<int> GetEnumerator()
{
return new OneToThree.OneToThreeEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new OneToThree.OneToThreeEnumerator();
}
}
class Program
{
static void Main(string[] args)
{
var onetothreeclass = new OneToThree();
var itor = onetothreeclass.GetEnumerator();
while (itor.MoveNext())
{
Console.WriteLine(itor.Current);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static IEnumerable<int> GetOneToThreeClass()
{
int _current = 1;
while (_current <= 3)
{
yield return _current++;
}
}
static void Main(string[] args)
{
var onetothreeclass = GetOneToThreeClass();
var itor = onetothreeclass.GetEnumerator();
while (itor.MoveNext())
{
Console.WriteLine(itor.Current);
}
}
}
}
class Program
{
//这个方法会创建一个匿名迭代器类,等价之前的代码
static IEnumerable<int> GetOneToThreeClass()
{
int _current = 1;
while (_current <= 3)
{
yield return _current++;
}
}
static void Main(string[] args)
{
GetOneToThreeClass();//疑问1;
GetOneToThreeClass().ToArray();//疑问2
foreach (int x in GetOneToThreeClass())
{
Console.WriteLine(x);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class OneToThree : IEnumerable<int>
{
public class OneToThreeEnumerator : IEnumerator<int>, IEnumerator
{
private int _current = 0;
public int Current
{
get { return _current; }
}
public void Dispose()
{
//这里我们不需要清理
}
object System.Collections.IEnumerator.Current
{
get { return _current; }
}
public bool MoveNext()
{
_current++;
return _current <= 3;
}
public void Reset()
{
_current = 0;
}
}
public IEnumerator<int> GetEnumerator()
{
return new OneToThree.OneToThreeEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new OneToThree.OneToThreeEnumerator();
}
}
class Program
{
static void Main(string[] args)
{
foreach (int x in new OneToThree())
{
Console.WriteLine(x);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
//这个方法会创建一个匿名迭代器类,等价之前的代码
static IEnumerable<int> GetOneToThreeClass()
{
int _current = 1;
while (_current <= 3)
{
yield return _current++;
}
}
static void Main(string[] args)
{
foreach (int x in GetOneToThreeClass())
{
Console.WriteLine(x);
}
}
}
}