简体   繁体   中英

Initialization Error in C#

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)

更改SubSet=new List<int>().AddRange(one)SubSet=new List<int>(one)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM