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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:55:23+00:00 2026-05-28T04:55:23+00:00

Today I was surprised to find that in C# I can do: List<int> a

  • 0

Today I was surprised to find that in C# I can do:

List<int> a = new List<int> { 1, 2, 3 };

Why can I do this? What constructor is called? How can I do this with my own classes? I know that this is the way to initialize arrays but arrays are language items and Lists are simple objects …

  • 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-28T04:55:23+00:00Added an answer on May 28, 2026 at 4:55 am

    This is part of the collection initializer syntax in .NET. You can use this syntax on any collection you create as long as:

    • It implements IEnumerable (preferably IEnumerable<T>)

    • It has a method named Add(...)

    What happens is the default constructor is called, and then Add(...) is called for each member of the initializer.

    Thus, these two blocks are roughly identical:

    List<int> a = new List<int> { 1, 2, 3 };
    

    And

    List<int> temp = new List<int>();
    temp.Add(1);
    temp.Add(2);
    temp.Add(3);
    List<int> a = temp;
    

    You can call an alternate constructor if you want, for example to prevent over-sizing the List<T> during growing, etc:

    // Notice, calls the List constructor that takes an int arg
    // for initial capacity, then Add()'s three items.
    List<int> a = new List<int>(3) { 1, 2, 3, }
    

    Note that the Add() method need not take a single item, for example the Add() method for Dictionary<TKey, TValue> takes two items:

    var grades = new Dictionary<string, int>
        {
            { "Suzy", 100 },
            { "David", 98 },
            { "Karen", 73 }
        };
    

    Is roughly identical to:

    var temp = new Dictionary<string, int>();
    temp.Add("Suzy", 100);
    temp.Add("David", 98);
    temp.Add("Karen", 73);
    var grades = temp;
    

    So, to add this to your own class, all you need do, as mentioned, is implement IEnumerable (again, preferably IEnumerable<T>) and create one or more Add() methods:

    public class SomeCollection<T> : IEnumerable<T>
    {
        // implement Add() methods appropriate for your collection
        public void Add(T item)
        {
            // your add logic    
        }
    
        // implement your enumerators for IEnumerable<T> (and IEnumerable)
        public IEnumerator<T> GetEnumerator()
        {
            // your implementation
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    Then you can use it just like the BCL collections do:

    public class MyProgram
    {
        private SomeCollection<int> _myCollection = new SomeCollection<int> { 13, 5, 7 };    
    
        // ...
    }
    

    (For more information, see the MSDN)

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

Sidebar

Related Questions

Somehow I never noticed until today that C++ supports nested classes. This surprised me
I was very surprised to get this error today, as it's one that I've
I was very surprised to get this error today, as it's one that I've
Today I happens to find that one C# class can inherit one interface both
We were surprised to learn today that threads waiting on a ManualResetEvent continue waiting
Today, I have read that command's object in WPF can be serialized. And I'm
I was surprised to learn today that PHP is used widely in high-traffic websites.
So far I thought that the following syntax was invalid, int B[ydim][xdim]; But today
I was quite surprised to find inconsistent results today from the following MySQL query:
I came across some code today that surprised me. A variable was defined (outside

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.