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!

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"}}, } } }

c#

6/1/2020 10:51:27 AM

Sudha

1 Answer

New 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....