自定义枚举/集合什么时候有用?
在访问不同的网站以尝试了解使用自定义枚举的实时示例后,我放弃了这一行。我有例子。但它们让我陷入困惑。
示例
Take 1
class NumberArray
{
public int[] scores;
public NumberArray()
{
}
public NumberArray(int[] scores)
{
this.scores = scores;
}
public int[] Scores
{
get {return scores;}
}
}
Take 2
public class Enumerator : IEnumerator
{
int[] scores;
int cur;
public Enumerator(int[] scores)
{
this.scores = scores;
cur = -1;
}
public Object Current
{
get {
return scores[cur];
}
}
public void Reset()
{
cur = -1;
}
public bool MoveNext()
{
cur++;
if (cur < scores.Length)
return true;
else return false;
}
}
public class Enumerable : IEnumerable
{
int[] numbers;
public void GetNumbersForEnumeration(int[] values)
{
numbers = values;
for (int i = 0; i < values.Length; i++)
numbers[i] = values[i];
}
public IEnumerator GetEnumerator()
{
return new Enumerator(numbers);
}
}
Main
static void Main()
{
int[] arr = new int[] { 1, 2, 3, 4, 5 };
NumberArray num = new NumberArray(arr);
foreach(int val in num.Scores)
{
Console.WriteLine(val);
}
Enumerable en = new Enumerable();
en.GetNumbersForEnumeration(arr);
foreach (int i in en)
{
Console.WriteLine(i);
}
Console.ReadKey(true);
}
在 Take 2 中,我按照自定义迭代来迭代与以下相同的整数数组我在 1. 为什么我要拐弯抹角地使用自定义迭代来迭代整数?
可能我错过了实时自定义迭代需求。您能向我解释一下我无法使用现有迭代工具完成的任务吗? (我刚刚完成学业,所以给我一个简单的例子,以便我能够正确理解)。
更新: 我从某个网站上获取了这些示例。该代码没有什么特别的,即使不使用自定义迭代,我们也可以非常简单地实现它,我的兴趣是了解自定义迭代非常方便的真实场景。
I am dropping this line after having visited different websites to try understand real time example of using custom enumeration. I got examples. But they lead me to confusion.
Example
Take 1
class NumberArray
{
public int[] scores;
public NumberArray()
{
}
public NumberArray(int[] scores)
{
this.scores = scores;
}
public int[] Scores
{
get {return scores;}
}
}
Take 2
public class Enumerator : IEnumerator
{
int[] scores;
int cur;
public Enumerator(int[] scores)
{
this.scores = scores;
cur = -1;
}
public Object Current
{
get {
return scores[cur];
}
}
public void Reset()
{
cur = -1;
}
public bool MoveNext()
{
cur++;
if (cur < scores.Length)
return true;
else return false;
}
}
public class Enumerable : IEnumerable
{
int[] numbers;
public void GetNumbersForEnumeration(int[] values)
{
numbers = values;
for (int i = 0; i < values.Length; i++)
numbers[i] = values[i];
}
public IEnumerator GetEnumerator()
{
return new Enumerator(numbers);
}
}
Main
static void Main()
{
int[] arr = new int[] { 1, 2, 3, 4, 5 };
NumberArray num = new NumberArray(arr);
foreach(int val in num.Scores)
{
Console.WriteLine(val);
}
Enumerable en = new Enumerable();
en.GetNumbersForEnumeration(arr);
foreach (int i in en)
{
Console.WriteLine(i);
}
Console.ReadKey(true);
}
In take 2, I followed the custom iteration to iterate the same integer array as I did in
take 1. Why should I beat about the bush to iterate an integer by using custom iteration?
Probably I missed out the real-time custom iteration need. Can you explain me the task which I can't do with existing iteration facility? (I just finished my schooling, so give me a simple example, so that I can understand it properly).
Update :
I took those examples from some site. There is nothing special in that code, we can achieve it very simply even without using custom iteration, my interest was to know the real scenario where custom iteration is quite handy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您迭代的资源不是预先加载到内存中,而是在每次迭代中根据需要获取时,自定义迭代器非常有用。例如,在 LINQ to SQL 中,当您执行以下操作时:
在
foreach
循环的每一步中,都会查询数据库中的Employees 表以获取下一条记录。这样,如果您提前退出循环,您就不会读取整个表,而只会读取您需要的记录。请参阅此处的实际示例:枚举文件中的行。在这里,每行都是在
foreach
循环的每次迭代中从文件中读取的。 (顺便说一句,.NET Framework 4 提供了相同的内置功能)。Custom iterators are useful when the resources you are iterating are not pre-loaded in memory, but rather obtained as needed in each iteration. For example, in LINQ to SQL, when you do something like:
in each step of the
foreach
loop, the Employees table in the database is queried to get the next record. This way, if you exit the loop early, you haven't read the whole table, but only the records you needed.See here for a real-world example: enumerate lines on a file. Here, each line is read from the file on each iteration of the
foreach
loop. (as a side note, .NET Framework 4 offers this same functionality built-in).老实说,我不完全确定你在问什么,但如果你担心返回数组(如 take 1)和返回迭代器(如 take 2)之间的区别,这是我的答案。
返回数组和返回迭代器之间有很大区别。返回数组意味着返回内部状态(除非您将副本作为返回的一部分,但这很昂贵)。如果你返回一个迭代器,你只是返回一个抽象,这将让调用者迭代状态。这也意味着,如果需要,您可以进行惰性评估(如果您返回数组,则显然无法执行此操作)。
此外,由于 C# 2 中对迭代器的支持,实际上没有理由手动实现 IEnumerable。请参阅此问题了解更多详细信息有人可以揭开yield关键字的神秘面纱吗?
To be honest, I am not entirely sure what you're asking, but if you're concerned about the difference between returning an array (as in take 1) and returning an iterator (take 2), here is my answer.
There a big difference between returning an array and returning an iterator. Returning an array means returning internal state (unless you make a copy as part of the return, but that is expensive). If you return an iterator, you just return an abstraction, that will let callers iterate state. This also means, that you can do lazy evaluation if you need to (something you obviously can't do if you're returning an array).
Also, with the support for iterators in C# 2, there is really no reason to implement IEnumerable by hand. Please see this question for additional detail Can someone demystify the yield keyword?
正如过去自定义迭代对我有用的另一个例子是提供一个通用抽象来递归枚举树结构中的所有节点......
Just as another example where custom iteration has been useful to me in the past was to provide a common abstraction to recursively enumerate all nodes in a tree structure...