Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 748169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:17:22+00:00 2026-05-14T14:17:22+00:00

I want to pass a List into a method, but I only want it

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:17:22+00:00Added an answer on May 14, 2026 at 2:17 pm

    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:

    MyType myType = new MyType();
    MyMethod(new List<MyType>{ myType }); 
    
    MyMethod(new List<MyType>{ new MyType(), new MyType() }); 
    
    MyMethod(new List<MyType>{ new MyType() }); 
    

    ========================
    Short answer: You are missing the parenthesis.

    MyType myType = new MyType();
    MyMethod(new List<MyType>(){ myType }); 
    

    or if you don’t need the variable named myType around beyond inserting (such that it will only be used from the list)

    MyMethod(new List<MyType>(){ new MyType(), new MyType() }); 
    

    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:

    MyMethod(new List<MyType>(){ new MyType() }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass a property list of a class to a function. with
I have a list consisting of tuples, I want to pass each tuple's elements
I want pass a class-instace to a method of an other and be sure
I want to pass the id from one action to the next action, but
I Have an Array of users that i want to pass into a view
I'm looking to combine the contents of two string arrays, into a new list
I want to pass a list of ids '' or '1,2,3,4,5,6,7' to a Stored
i want to pass the value of a datetimepicker to my Codebehind(C#) and save
I want to pass data from client side to server-side. I am using jQuery
I want to pass an IEnumerable collection from view to controller using JQuery from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.