使用一个对象在for循环中插入数字,然后通过getenumerator进行迭代 [英] Using one object to insert numbers in for loop and then iterating via getenumerator

查看:51
本文介绍了使用一个对象在for循环中插入数字,然后通过getenumerator进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我用google搜索有关编码问题的答案,我遇到了代码项目



我一直在研究C#Generics ..我有这段代码。我能够理解foreach循环迭代多个对象的一点,但在这个代码中只有一个CustomList< int>的对象。类创建列表1是,然后列表1在for循环使用通过Nodes..but添加(插入)号的GetEnumerator如何工作here..we只有一个对象,它是列表1,用于一遍又一遍在for循环中插入数...如果我明确说明问题,请指导



我尝试过的事情:



<预郎= CS> <跨度类= 代码关键字>命名空间 GenericsusingLinkedList_25_03
{
<跨度类= 代码注释> / / 测试CustomList
TestCustomList
{
static void Main()
{
// 声明一个int类型的List,然后循环遍历List
CustomList< int> list1 = new CustomList< int>();

for int x = 1 ; x < = 3 ; x ++)
{
list1.Add(x);
}

foreach int i in list1)
{
System.Console.Write(i + );
}

Console.ReadKey();
}
}
public class CustomList< t>
{
// 字段
private 节点head = null ;

// 嵌套类在T
上也是通用的 private class 节点
{
// 字段
private 节点接下来;

// T作为私人会员数据类型
private T数据;


// 属性
public 节点下一个
{
获取
{
返回下一步;
}
set
{
next = value ;
}
}
// T作为Property的返回类型
public T数据
{
获取
{
返回数据;
}
set
{
data = value ;
}
}
// 非泛型构造函数中使用的T
public 节点(T pData)
{
。 next = null ;
.data = pData;
}
}
// T作为方法参数类型:
public void 添加(T pType)
{
Node n = new 节点(pType);

n.Next = head; // 当前节点的下一个被分配上一个节点的头
this .head = n; // 当前的头部与n相同
}
// 在列表中启用foreach
public 的IEnumerator< T> GetEnumerator()
{
Node current = head;
while (当前!= null
{
yield return current.Data;
current = current.Next;
}
}
}
}

解决方案
key语句是<无线电通信/>

  yield   return  current.Data; 



允许迭代器一次接收一个集合的每个元素。请参阅收率(C#参考) [ ^ ]。


Hi there,

I googled out about getting answer for your coding question and i came across codeproject

I have been studying C# Generics.. and i have this piece of code. i am being able to comprehend a point that foreach loop iterates over number of objects but here in this code only one object of CustomList<int> class is created that is list1 and then list1 is used in forloop to add(insert) numbers via Nodes..but how GetEnumerator works here..we had only one object that is list1 that is used over and over in for loop to insert numbers...please guide if I am clearly stating the problem

What I have tried:

namespace GenericsusingLinkedList_25_03
{
    // Test the CustomList
    class TestCustomList
    {
        static void Main()
        {
            // Declare a List of type int, then loop through the List
            CustomList<int> list1 = new CustomList<int>();

            for (int x = 1; x <= 3; x++)
            {
                list1.Add(x);
            }

            foreach (int i in list1)
            {
              System.Console.Write(i + " ");
            }
           
            Console.ReadKey();
        }
    }
    public class CustomList<t>
    {
        // Fields
        private Node head=null;

        // The nested class is also generic on T
        private class Node
        {
            // Fields
            private Node next;

            // T as private member data type
            private T data;

            
            // Properties
            public Node Next
            {
                get
                {
                    return next;
                }
                set
                {
                    next = value;
                }
            }
            // T as return type of the Property
            public T Data
            {
                get
                {
                    return data;
                }
                set
                {
                    data = value;
                }
            }
            // T used in non-generic constructor
            public Node(T pData)
            {
                this.next = null;
                this.data = pData;
            }
        }
         // T as method parameter type:
        public void Add(T pType)
        {
            Node n = new Node(pType);

            n.Next = head;    //next of current node is assigned head of previous node
            this.head = n;    //head of current is same as n
        }
        // Enables foreach on the List
        public IEnumerator<t> GetEnumerator()
        {
            Node current = head;
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }     
    }       
}

解决方案

The key statement is

yield return current.Data;


which allows an iterator to receive each element of a collection one at a time. see yield (C# Reference)[^].


这篇关于使用一个对象在for循环中插入数字,然后通过getenumerator进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆