遍历类c#中的列表

问题描述 投票:0回答:1

我有以下课程:

    public class SkinList : INotifyPropertyChanged
{
    public class Coord
    {
        public int x { get; set; }
        public int y { get; set; }
    }

    public class Location
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public Coord Coords { get; set; }
        public string CodeNumber { get; set; }
        public string Description { get; set; }
    }

    public class Skin
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public List<Location> Locations { get; set; }
    }

    public class RootObject
    {
        public List<Skin> Skins { get; set; }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

在其他地方,我试图使用一个foreach语句来循环访问Skin类中的所有位置:

    bool OnPoint(Point DotExists, SkinListModel.Skin skin)
    {
        foreach (SkinListModel.Location x in skin)
        {

        }
        return true;
    }

我遵循了这里的建议:How to make the class as an IEnumerable in C#?

    public class Skin : IEnumerable<Location>
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public List<Location> Locations { get; set; }
        public IEnumerator<Location> GetEnumerator()
        {
            return Locations.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

但我遇到的错误包括:

错误CS0738'SkinListModel.Skin'没有实现接口成员'IEnumerable.GetEnumerator()'。 'SkinListModel.Skin.GetEnumerator()'无法实现'IEnumerable.GetEnumerator()',因为它没有匹配的返回类型'IEnumerator'。

和:

错误CS0305使用通用类型'IEnumerable<T>'需要1个类型参数

我似乎无法找到解决方案,有什么想法吗?

c# list class ienumerable
1个回答
0
投票

您的代码看起来不错,因此您可能缺少using指令:using System.Collections;

编译器无法解析名称IEnumerator

© www.soinside.com 2019 - 2024. All rights reserved.