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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:35:26+00:00 2026-06-01T07:35:26+00:00

I’m just thinking about the styling and performance. Previously I used to write something

  • 0

I’m just thinking about the styling and performance. Previously I used to write something like,

var strings = new List<string> { "a", "b", "c" };
var ints = new List<int> { 1, 2, 3};

But now I tend to like this style more,

var strings = new [] { "a", "b", "c" }.ToList();
var ints = new [] { 1, 2, 3}.ToList();

I prefer the second style, but now considering – is it really worth to write it like that or maybe it’s not that efficient and requires more operations?

  • 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-01T07:35:28+00:00Added an answer on June 1, 2026 at 7:35 am

    I disagree with Darin: they’re not equivalent in terms of performance. The latter version has to create a new array, and ToList will then copy it into the new list. The collection initializer version is equivalent to:

    var tmp = new List<int>();
    tmp.Add(1);
    tmp.Add(2);
    tmp.Add(3);
    var ints = tmp;
    

    Assuming the list starts off with a large enough buffer, that won’t require any further allocation – although it will involve a few method calls. If you do this for a very large number of items, then it will require more allocations than the ToList version, because it will copy the items as it goes.

    The performance difference is likely to be negligible, but it’s non-zero (and not clearly better in either direction – there are fewer calls in the array version, but more allocation).

    I would concentrate more on style than performance unless you have a reason to suspect that the difference is significant, in which case you should measure rather than just guessing.

    Personally I prefer the first form – I think it makes it clearer that you’re using a list right from the start. Another alternative would be to write your own static class:

    public static class Lists
    {
        public static List<T> Of<T>(T item0)
        {
            return new List<T> { item0 };
        }
    
        public static List<T> Of<T>(T item0, T item1)
        {
            return new List<T> { item0, item1 };
        }
    
        public static List<T> Of<T>(T item0, T item1, T item2)
        {
            return new List<T> { item0, item1, item2 };
        }
    
        ... as many times as you really care about, then ...
    
        public static List<T> Of<T>(params T[] items)
        {
            return items.ToList();
        }
    }
    

    Then you can write:

    var ints = Lists.Of(1);
    var ints = Lists.Of(1, 2, 3);
    var ints = Lists.Of(1, 2, 3, 5, 6, 7, 8); // Use the params version
    

    This still makes it clear that you’re using lists, but takes advantage of type inference.

    You may well consider it overkill though 🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

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.