【问题标题】:Static factory method - Separate Class to hold data in a list静态工厂方法 - 单独的类以将数据保存在列表中
【发布时间】:2015-12-02 10:58:17
【问题描述】:

我有这个类用于创建值列表

public class map
{
    private static List<map> mapValues = new List<map>();
    public static IEnumerable<map> AllInstances
    {
        get { return mapValues; }
    }

    public int Row { get; set; }
    public int Column { get; set; }
    public Object theobject { get; set; }

    private map()   // Private ctor ensures only a member
    {               // function can create a new map
    }
    public static map Create()
    {
        var mv = new map();
        mapValues.Add(mv);
        return mv;
    }

    public static void Delete(map itemToRemove)
    {
        mapValues.Remove(itemToRemove);
    }
}

我的这个课程来自this comment

但是当我来到var Foundit = MyData.AllInstances.FirstOrDefault(md =&gt; md.Device == "blah");的部分时 Myclass 没有这个 FirstOrDefault

这个列表的想法是有一个类似网格/地图的系统,用于在 WPF 画布中放置对象。

让这个工作我缺少什么?

【问题讨论】:

标签: c# .net wpf extension-methods


【解决方案1】:

FirstOrDefault 是一个扩展方法,所以尝试添加这个命名空间:

using System.Linq;

另外一点:至于AllInstances是一个静态类成员,你不必像MyData这样从map实例调用它,只需从像这样的类:

map.AllInstances  //access AllInstances through the class name

【讨论】:

  • 谢谢,我现在可以像其他评论一样使用它了。
【解决方案2】:

在要使用.FirstOrDefault(...)的文件顶部添加using System.Linq;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多