You can declare a Dictionary class like this:

 var d = new Dictionary<int, string> { {1, "..."}, ... }; 

How to make it so that I can initialize my class in this way?

 List<MyClass> l = new List { {...} // Инициализация моего класса } 

    1 answer 1

    In order for such an implicit initializer to work, you need to add the Add method to the class being initialized with the parameters corresponding to those in square brackets. For example, if you have a class MyClass, which has a constructor from two lines and you want to easily initialize the list of such classes, you will have to create a class for the list:

     class MyClassList : List<MyClass> { public void Add(string s1, string s2) { Add(new MyClass(s1, s2)); } } 

    Now you get the opportunity to initialize your list like this:

     List<MyClass> l = new MyClassList() { {"a", "b"}, {"c", "d"} }; 
    • Thank you - Donil