Initializing Generic.List in C #

In C #, I can initialize a list using the following syntax.

List<int> intList= new List<int>() { 1, 2, 3 }; 

I would like to know how the {} syntax works, and if it has a name. There is a constructor that accepts IEnumerable , you can call it that.

 List<int> intList= new List<int>(new int[]{ 1, 2, 3 }); 

This seems more "standard." When I deconstruct the default constructor for a list, I only see

 this._items = Array.Empty; 

I would like to be able to do this.

 CustomClass abc = new CustomClass() {1, 2, 3}; 

And be able to use list 1, 2, 3 . How it works?

Refresh

John Skeet answered

It calls the constructor without parameters, and then call Add:

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

I understand what I'm doing. I want to know how to do this. How does this syntax know to call the Add method?

Update

I know how to accept John Skeet’s answer. But, the example with strings and ints is awesome. Also a very useful MSDN page is:

+46
generics constructor c #
Apr 15 '09 at 14:34
source share
7 answers

This is called a collection initializer. It calls the constructor without parameters, and then calls Add:

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

Requirements for type:

  • It implements IEnumerable
  • It has Add overloads that are suitable for the types of arguments that you supply. You can provide several arguments in braces, in which case the compiler is looking for the Add method with several parameters.

For example:

 public class DummyCollection : IEnumerable { IEnumerator IEnumerable.GetEnumerator() { throw new InvalidOperationException("Not a real collection!"); } public void Add(string x) { Console.WriteLine("Called Add(string)"); } public void Add(int x, int y) { Console.WriteLine("Called Add(int, int)"); } } 

Then you can use:

 DummyCollection foo = new DummyCollection { "Hi", "There", { 1, 2 } }; 

(Of course, usually you want your collection to implement IEnumerable correctly ...)

+64
Apr 15 '09 at 14:36
source share
— -

Read Object and Collection Initializers (C # Programming Guide) . Basically you can do this with every custom type that is a list (implements IEnumerable).

+7
Apr 15 '09 at 14:36
source share

They are called collection initializers (also see here ), and the way they work is to look for the Add() method, which can execute its bets. It calls Add() for each of the integers that you have in braces.

Finding the Add() method is pure compiler magic. It is hard-coded to search for a method of this name.

+4
Apr 15 '09 at 14:37
source share

The name you are looking for is "Collection Initializer." It works under the hood, looking for the Add method by type of collection and calling it for each value you specify.

Read more: Initializers of objects and collections (C # Programming Guide)

+2
Apr 15 '09 at 14:38
source share

I find this a shortcut to the .Add method. I never tried to override it, so I don't know if this is possible.

+1
Apr 15 '09 at 14:36
source share

As far as I know, adding elements through object initialization searches for the Add method. So, since the List <int> will have a void Add (int), it will work.

To use it in your class just add

 class CustomClass { public void Add(int num) { // Your code here } } 

Your class should implement IEnumerable, as Hallgrim pointed out.

0
Apr 15 '09 at 14:37
source share

Actually the .Add method is .Add . This means that it calls .Add for each element in brackets inside the constructor.

0
Apr 15 '09 at 14:41
source share



All Articles