J
J
jekanok2021-12-11 10:21:42
C++ / C#
jekanok, 2021-12-11 10:21:42

Populate list instance dynamically?

Hi there is a code that has a List where it should be filled example with data new Employee(0, 200)

using System;
using System.Collections.Generic;
public class Employee
{
    public int place;
    public int score;
  

  
  
    public Employee(int place, int score)
    {
        this.place = place;
        this.score = score;
    }
  
  

 
    public override string ToString() {
        return "" + place + "";
    }
}
 
public class Example
{
    public static void Main()
    {
        Employee first = new Employee(0, 200);
  //тут заполняем лист 
    List<List<object>> list = new List<List<object>>();
    list.Add(new List<object>{});
    list[0].Add(new Employee(0, 200));
    
      // сюда его передаем
        List<Employee> employees = new List<Employee>(){  list };
    
    
 
        employees.Sort(delegate(Employee x, Employee y) {
            return x.score.CompareTo(y.score);
        });
    
 		employees.Reverse();

    
        Console.WriteLine(String.Join(Environment.NewLine, employees));
    }
}


Thanks for the answer!)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-12-11
@jekanok

well, it’s not a beautiful code at all, but if you really really need it, then write it like this:

var employeeList = objListList[0].Select(x=>x as Employee).ToList();

Casting is necessary, because you have a list of objects

B
BasiC2k, 2021-12-11
@BasiC2k

Enough:
List list = new List();
list.Add(new Employee(0, 200));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question