I have created a class student with three properties like this
public class Student
{
public int age;
public string name;
public string course;
public Student(int age , string name , string course)
{
this.age = age;
this.course = course;
this.name = name;
}
List<Student> school = new List<Student>(
new Student(12,"ram","ece"));
);
}
what I am trying to do is, I am adding student details manually to student class
but I am getting this error at this line
new Student(12,"ram","ece"));
Error : cannot convert from
windowsapplication.studenttosystems.Collections.Generic.IEnumerable<windowsapplication.Student>
Why is this happening?
The syntax you have used is trying to pass a new
Studentto the constructor ofList<Student>– there is no such constructor, hence the error.You have a small syntax error. This should work:
The syntax for collection initializer is with
{}not().