2

There's a C# feature which I don't know the term for, and as such haven't been able to find documentation on it.

What is the name for the "AddRange-like" syntax that lets you add elements to a collection during initialization? I'm referring specifically to the case documented in the code below, where we are not calling the constructor (and are unable to due to the property not having a setter), but instead seem to just be calling AddRange on the collection.

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        TestClass a = new TestClass()
        {
            // What is the name for this feature??
            List =
            {
                0,
                10,
                20,
                30
            }
        };
        Console.WriteLine(a.List.Count);
    }

    public class TestClass
    {
        private List<int> _list = new List<int>();
        public List<int> List { get { return _list; } }
    }
}

Also, am I understanding the semantics of this syntax correctly? It is just allowing us a to shorthand AddRange/multiple-Add syntax on a collection, correct?

.NET Fiddle here, should anyone want to run the sample code: https://dotnetfiddle.net/i01HYv

bsinky
  • 515
  • 5
  • 15
  • 1
    It's a collection initializer: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers – Jon B Feb 21 '18 at 16:26
  • I'm fairly certain the syntax is just referred to as a collection initializer. It does in fact compile down to calling `.Add` on the collection, which is why the syntax won't work with, say, an array, and will blow up if the collection is uninitialized. – Jonathon Chase Feb 21 '18 at 16:26
  • 4
    Considering you refer to the name of the feature in your question, why are you asking what the name of the feature is? – Servy Feb 21 '18 at 16:28
  • @Servy My confusion stemmed from the docs on initializers not being clear that the constructor call is optional during collection initialization, allowing you to use collection initializers for properties that you do not have Set access to. – bsinky Feb 21 '18 at 16:31
  • @bsinky Calling a constructor *isn't* optional. You *always* call a constructor. If you're curious about a specific behavior of collection initializers then why did you ask what the feature is called, instead of asking about the specific behavior you wanted to learn about? – Servy Feb 21 '18 at 16:44
  • 1
    @Servy - to be completely accurate, the class constructor is not always called in all circumstances. object obj = System.Runtime.Serialization.FormatterServices .GetUninitializedObject(t); does not call the constructor. I know the OP was not asking about it, but thought it would be interesting to point out. – PhillipH Feb 21 '18 at 20:09

1 Answers1

2

Its the Object Initializer or Collection Initializer syntax introduced in C# 3.0

PhillipH
  • 6,182
  • 1
  • 15
  • 25