Quick way to create a list of values in C#?

后端 未结 10 2346
梦如初夏
梦如初夏 2021-01-31 13:14

I\'m looking for a quick way to create a list of values in C#. In Java I frequently use the snippet below:

List l = Arrays.asList(\"test1\",\"test2         


        
10条回答
  •  无人共我
    2021-01-31 13:32

    If you want to create a typed list with values, here's the syntax.

    Assuming a class of Student like

    public class Student {
       public int StudentID { get; set; }
       public string StudentName { get; set; }
     }   
    

    You can make a list like this:

    IList studentList = new List() { 
                    new Student(){ StudentID=1, StudentName="Bill"},
                    new Student(){ StudentID=2, StudentName="Steve"},
                    new Student(){ StudentID=3, StudentName="Ram"},
                    new Student(){ StudentID=1, StudentName="Moin"}
                };
    

提交回复
热议问题