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 8942427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:30:47+00:00 2026-06-15T11:30:47+00:00

In C# i can write var y = new List<string>(2) { x , y

  • 0

In C# i can write

var y = new List<string>(2) { "x" , "y" };

to get a List with “x” and “y” initialized.

How do I declare a class to accept this initialization syntax?

I mean, I want to write:

var y = new MyClass(2, 3) { "x" , "y" };
  • 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-06-15T11:30:48+00:00Added an answer on June 15, 2026 at 11:30 am

    Look at section 7.6.10.3 of the C# spec:

    The collection object to which a collection initializer is applied
    must be of a type that implements System.Collections.IEnumerable or a
    compile-time error occurs. For each specified element in order, the
    collection initializer invokes an Add method on the target object with
    the expression list of the element initializer as argument list,
    applying normal overload resolution for each invocation. Thus, the
    collection object must contain an applicable Add method for each
    element initializer.

    A very simple example of this:

       class AddIt : IEnumerable
       {
          public void Add(String foo) { Console.WriteLine(foo); }
    
          public IEnumerator GetEnumerator()
          {
             return null; // in reality something else
          }
       }
    
       class Program
       {
          static void Main(string[] args)
          {
             var a = new AddIt() { "hello", "world" };
    
             Console.Read();
          }
       }
    

    This will print “hello”, followed by “world” to the console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I write something like this: var img = $(new Image()).attr('src', image.src); How can
Using the C# compilers query comprehension features, you can write code like: var names
Can I write the if else shorthand without the else ? var x=1; x==2
Hello all I was hoping to get some help with inported this class to
If I have an object public class Car { public List<UpdatePart> updatePartList {get; set;}
Wasn't sure how to write a good title for this question... :) I'm new
I have the following class: public class MySeat { public string Section { get;
I have below class : public class ControllerSecurityModel { public string ControlleName { get;
Given a list of 'addresses' (String) I'm able to get the geocoding (latitude and
You can write //google.com instead of http://google.com and https://google.com where // is relative to

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.