Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add Item to List at creating object

Tags:

c#

list

How do I create a List in a reference?

MyClass classA  = new MyClass();

myFuction(new List<MyClass>( ??? )) 

How Can I add classA to that new List?

like image 245
kmxillo Avatar asked Dec 08 '22 11:12

kmxillo


1 Answers

Use

 new List<MyClass>() {classA};

For collections, the elements inside the brackets will be executed using the add method (for "regular classes" they will be interpreted as properties)

like image 156
SJuan76 Avatar answered Dec 11 '22 08:12

SJuan76