Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB6到C#:IUnknown_C#_Properties_Vb6_Vb6 Migration_Iunknown - Fatal编程技术网

VB6到C#:IUnknown

VB6到C#:IUnknown,c#,properties,vb6,vb6-migration,iunknown,C#,Properties,Vb6,Vb6 Migration,Iunknown,我试图将VB6中的一个属性转换为C#。详情如下: Public Property Get NewEnum() As IUnknown 'this property allows you to enumerate 'this collection with the For...Each syntax Set NewEnum = m_coll.[_NewEnum] End Property m_coll是一个私有变量,它现在是一个ArrayList而不是前一个集合 m_co

我试图将VB6中的一个属性转换为C#。详情如下:

Public Property Get NewEnum() As IUnknown
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = m_coll.[_NewEnum]
End Property
m_coll
是一个私有变量,它现在是一个
ArrayList
而不是前一个
集合

m_coll
正在填充我自己的一个类对象。如您所见,此属性的类型为IUnknown


在这一点上,我可能没有正确地思考,但是如果您希望能够对一个类进行foreach(就像在vb6中将NewEnum()作为IUnknown公开一样),那么在C#?

中是否有类似于这种属性的属性,您可以让您的类实现IEnumerable-例如:

   public class MyClass : IEnumerable 
    {
        private List<string> items = new List<string>();

        public MyClass()
        {
            items.Add("first");
            items.Add("second");
        }


        public IEnumerator GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }

为了简单起见,我使用了
List
,但是你可以使用
List

你在考虑IEnumerable吗?这允许您对集合使用foreach语句。您不应使用
ArrayList
。改用通用的
列表
。我已经弄明白了你的答案,但我仍然会告诉你答案;)谢谢谢谢对不起,我没有更快:)
  MyClass myClass =new MyClass();
            foreach (var itm in myClass)
            {
                Console.WriteLine(itm);
            }