C# 中的初始化错误

发布于 2024-08-08 09:39:22 字数 1335 浏览 6 评论 0原文

我有一个类型设置如下,

class Program
{
   static void Main()
   {
      IEnumerable<int> one = new int[] { 1, 2, 3, 4, 5, 6, 7 };
      IEnumerable<int> two = new int[] { 12, 34, 56, 7, 8 };
  MySet[] sets
  = new MySet[]
    { 
     new MySet{ MySetID =100,
     MySubSet=new MySubSet{SubSet=new List<int>().AddRange(one), SubSetID=1212}},

     new MySet{ MySetID =101,
     MySubSet=new MySubSet{SubSet=new List<int>().AddRange(two), SubSetID=1414}}
    };

 }
  }

class MySet
{
    int mySetID; 
    MySubSet subset = new MySubSet();
    public int MySetID
    { 
        get { return mySetID; }
        set { mySetID = value; }
    }
    public MySubSet MySubSet 
     {
         get { return subset; } 
         set { subset = value; }
    } 
}

class MySubSet
{
    int subsetID;
    List<int> subset = new List<int>();
    public List<int> SubSet
    {
        get{ return subset; }
        set {subset = value;}
    }
    public int SubSetID 
    {
        get { return subsetID; }
        set { subsetID = value; }
    }
}

遇到错误

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<int>'

我在 Main() 声明中

MySubSet=new MySubSet{SubSet=new List<int>().AddRange(one)

I have a Type set up as follow

class Program
{
   static void Main()
   {
      IEnumerable<int> one = new int[] { 1, 2, 3, 4, 5, 6, 7 };
      IEnumerable<int> two = new int[] { 12, 34, 56, 7, 8 };
  MySet[] sets
  = new MySet[]
    { 
     new MySet{ MySetID =100,
     MySubSet=new MySubSet{SubSet=new List<int>().AddRange(one), SubSetID=1212}},

     new MySet{ MySetID =101,
     MySubSet=new MySubSet{SubSet=new List<int>().AddRange(two), SubSetID=1414}}
    };

 }
  }

class MySet
{
    int mySetID; 
    MySubSet subset = new MySubSet();
    public int MySetID
    { 
        get { return mySetID; }
        set { mySetID = value; }
    }
    public MySubSet MySubSet 
     {
         get { return subset; } 
         set { subset = value; }
    } 
}

class MySubSet
{
    int subsetID;
    List<int> subset = new List<int>();
    public List<int> SubSet
    {
        get{ return subset; }
        set {subset = value;}
    }
    public int SubSetID 
    {
        get { return subsetID; }
        set { subsetID = value; }
    }
}

I am getting error

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<int>'

inside Main() declaration

MySubSet=new MySubSet{SubSet=new List<int>().AddRange(one)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离去的眼神 2024-08-15 09:39:22

SubSet=new List().AddRange(one) 更改为 SubSet=new List(one)

Change your SubSet=new List<int>().AddRange(one) for SubSet=new List<int>(one)

可可 2024-08-15 09:39:22

尝试将 SubSet=new List().AddRange(one) 更改为 SubSet=new List(one)

Try changing SubSet=new List<int>().AddRange(one) to SubSet=new List<int>(one)

末蓝 2024-08-15 09:39:22

这是因为 AddRange 不返回 List,它什么也不返回。使用 Concat 方法而不是 AddRange。

It's because AddRange does not return a List, it returns nothing. Use the Concat method instead of AddRange.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文