Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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
C#类属性可枚举_C#_Wpf_Enumeration - Fatal编程技术网

C#类属性可枚举

C#类属性可枚举,c#,wpf,enumeration,C#,Wpf,Enumeration,我对类的枚举有问题。我在互联网和Stackoverflow上到处搜索,但我担心我有限的C#经验限制了我对确切情况和解决方案的认识。我的守则如下: public List<annotation> annotations = new List<annotation>(); public class annotation { public annotation(int pos, int x2, int y2, string artnr) { t

我对类的枚举有问题。我在互联网和Stackoverflow上到处搜索,但我担心我有限的C#经验限制了我对确切情况和解决方案的认识。我的守则如下:

public List<annotation> annotations = new List<annotation>();

public class annotation
{
    public annotation(int pos, int x2, int y2, string artnr)
    {
        this.ArtNr = artnr;
        this.Pos = pos;
        this.X2 = x2;
        this.Y2 = y2;
    }
    public string ArtNr;
    public int Pos;
    public int X2;
    public int Y2;


}

public void add_Anno(string artnr, int x2, int y2)
{
    annotations.Add(new annotation(0,x2,y2,artnr));
}
同时,我正在查看“微软新闻考试7-483”中的反映,看看这是否是一个解决方案


谢谢。

您只需使用
foreach
,它将迭代列表中的每个元素。无需使用反射:

foreach (var annotation in annotations)
{
    int position = annotation.Pos;
    string atrNr = annotation.ArtNr;
}
作为旁注,我建议你看看,也许研究一下

快速重构如下所示:

public class Annotation
{
    public Annotation(int pos, int x2, int y2, string artNr)
    {
        this.ArtNr = artNr;
        this.Pos = pos;
        this.X2 = x2;
        this.Y2 = y2;
    }

    public string ArtNr { get; private set; }
    public int Pos { get; private set; }
    public int X2 { get; private set; }
    public int Y2 { get; private set; }
}

如果您使用的是C#6,您可以删除
私有集

只要使用
foreach
,它就会迭代列表中的每个元素。无需使用反射:

foreach (var annotation in annotations)
{
    int position = annotation.Pos;
    string atrNr = annotation.ArtNr;
}
作为旁注,我建议你看看,也许研究一下

快速重构如下所示:

public class Annotation
{
    public Annotation(int pos, int x2, int y2, string artNr)
    {
        this.ArtNr = artNr;
        this.Pos = pos;
        this.X2 = x2;
        this.Y2 = y2;
    }

    public string ArtNr { get; private set; }
    public int Pos { get; private set; }
    public int X2 { get; private set; }
    public int Y2 { get; private set; }
}

如果您使用的是C#6,您可以删除
私有集

谢谢,首先我有一个可枚举错误,但似乎根本不需要添加枚举。谢谢,首先我有一个可枚举错误,但似乎根本不需要添加枚举。