代码之家  ›  专栏  ›  技术社区  ›  Zachary Scott

IEnumerable数据类型:如何创建空对象?

  •  0
  • Zachary Scott  · 技术社区  · 14 年前

    我有一个具有多个IEnumerable集合的对象:

    public class Product
    {
        public int id {get;set;}
        public string name {get;set;}
        IEnumerable<CrossSell> CrossSells {get;set;}
        IEnumerable<UpSell> UpSells {get;set;}
        IEnumerable<PricePromos> PricePromos {get;set;}
    }
    

    我需要创建一个空对象(所有属性都是空的,但不是空的)。 显然,我不能只创建一个可枚举项。

    除了创建一个全新的类,我如何才能做到这一点?

    2 回复  |  直到 12 年前
        1
  •  9
  •   Bern    12 年前

    Enumerable.Empty<T>() 或者只是一个新的零长度数组。

        2
  •  0
  •   Gabe Timothy Khouri    14 年前

    正如cdhowie所建议的,零元素数组也可以工作。语法是 new T[0] .