I have a doubt in list. If model class has a one list property then how can assign a value to that property ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a doubt in list. If model class has a one list property then how can assign a value to that property ?

If i do like below format i am getting nullreference error Public class student{pb int id;pb list<string>name} Public class fetch{ Public static void main(){ IList<student> studentList = new List<student>(){ new student(){id=1,name={"bill"}}, new student(){id=2,name={"steve"}}, } } }

1st Jun 2020, 10:51 AM
Sudha
Sudha - avatar
1 Answer
0
You need to assign those in a method or constructor... Public class student{ pb int id; pb list<string>name; student(int id, List<string> name) { this.id = id; this.name = name; } Public class fetch{ Public static void main(){ IList<student> studentList = new List<student>(){ new student(1,{"bill"}), new student(2,{"steve"}); } } } I think it need some more changes... Define fisrt list then pass that refference through as an argument.. But you are passing a implementation to list not properties. It won't possible I guess in c#. You can pass objects by assigning defaults by constructor as I shown....
1st Jun 2020, 6:30 PM
Jayakrishna 🇮🇳