I want to pass a List into a method, but I only want it to contain 1 item.
Is it possible for me to do this similar to
MyType myType = new MyType();
MyMethod(new List<MyType>{ myType }); // somehow add myType to the list as I'm creating it
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I was wrong, the short answer wasn’t missing parenthesis in the example as it was posted. There must have been some other typo because all of the following worked when I tested it:
========================
Short answer: You are missing the parenthesis.
or if you don’t need the variable named myType around beyond inserting (such that it will only be used from the list)
Note the example directly above inserts two items in the list. I wrote it that way to show multiple creations. If you just wanted one as you indicated in your question then this is what you should use: