Questions tagged [collection-initializer]

Collection Initializer is a C# syntactic sugar for compact declaration of collection objects with items.

is a syntactic sugar for compact declaration of collection objects with items.

Collection initializers can be used with types which implement interface and have applicable Add method. Collection initializers are described in §7.6.10.3 of C# Language Specification

48 questions
6
votes
2 answers

Can another thread see partially created collection when using collection initializer?

Imagine this C# code in some method: SomeClass.SomeGlobalStaticDictionary = new Dictionary() { {0, "value"}, }; Let's say no one is using any explicit memory barriers or locking to access the dictionary. If no optimization takes…
Palo
  • 1,051
  • 8
  • 12
5
votes
1 answer

How to implement a clean Custom Object Initializer for a Matrix class

I have a custom Matrix class that I would like to implement a custom object initializer similar to what a double[,] can use but can not seem to figure out how to implement it. Ideally I would like to have it look like this var m1 = new Matrix …
PlTaylor
  • 7,345
  • 11
  • 52
  • 94
4
votes
3 answers

Creating a New Class with Collection Initializers in C#

I have a class that I created. I would like to allow the class to be able to have a collection initializer. Here is an example of the class: public class Cat { private Dictionary catNameAndType = new Dictionary(); public Cat() { …
user489041
  • 27,916
  • 55
  • 135
  • 204
4
votes
1 answer

Initialize the Attributes property of a WebControl object using collection initializer

I want to initialize WebControl objects, inline, but for some fields this is a little bit tricky. For instance when I try to initialize the Attributes property of a TextBox object like this: using System.Web.UI.WebControls; Panel panel = new Panel()…
Chris
  • 1,213
  • 1
  • 21
  • 38
4
votes
1 answer

Empty collection initializer for list property results in null

When I run this code, it doesn't initialize ThisIsAList to an empty collection as I was expecting... instead ThisIsAList was null. void Main() { var thing = new Thing { ThisIsAList = {} }; Console.WriteLine(thing.ThisIsAList…
Jeff B
  • 8,572
  • 17
  • 61
  • 140
4
votes
1 answer

Object Initialization Not Working for Me

What am I missing here? I expected the following to work just fine: public class ProposalFileInfo { public int FileId { get; set; } public bool IsSupportDocument { get; set; } } // ... var attachments = new…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
3
votes
3 answers

Object initialization similar to List<> syntax

How can I define the class so that it could be initialized similarly like List: List list = new List(){ //this part }; e.g., this scenario: Class aClass = new Class(){ new Student(), new Student()//... };
Miro
  • 1,778
  • 6
  • 24
  • 42
3
votes
3 answers

C# Collection initialization syntax for use with custom matrix class?

I'm creating a custom matrix class that only has a single 2D array to hold everything (I know that a 1D array is faster and better, but that's not the point of this implementation) the thing is, I'd like to have a constructor, and be able to do…
Raxmo
  • 57
  • 5
3
votes
2 answers

How can you determine if the object initializer is the one calling the Add method?

I have a custom Vector class that can be initialized several ways. It has the following constructor to create a vector of a given length with all zeros. public Vector(int length) { ... } or it can use an object initializer by implementing…
PlTaylor
  • 7,345
  • 11
  • 52
  • 94
2
votes
1 answer

Collection initializers inside object initializers with default values

I just stumbled upon the following issue: class Settings { // Let's set some default value: { 1 } public ICollection AllowedIds = new List() { 1 }; } static void Main(string[] args) { var s = new Settings { …
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2
votes
1 answer

initialize collection-member on a class

I have a class with that has a collection-property: class MyClass { public MyCollection Coll { get; private set; } public MyClass() { this.Coll = new MyCollection(); } } class MyCollection : IList { ... } Now I´m creating two different…
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2
votes
1 answer

Asynchronous population of collection initializer

I would like to populate a collection using collection initializer that will call async methods: public class Diagnostics { public async Task> Get() => new List { await…
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
2
votes
0 answers

Collection initializer and TypeInitializationException vs IEnumerable

I have used collection initializer for a Dictionary and received a TypeInitializationException: public static Dictionary specialFolders = new Dictionary { // ... …
sharpener
  • 1,383
  • 11
  • 22
2
votes
1 answer

How to call a method with the C# collection initializer?

Case This morning I refactored some Logging method and needed to change a method's 'params' parameter in a normal array. Consequently, the call to the method had to change with an array parameter. I'd like the method call to change as less as…
Herman Cordes
  • 4,628
  • 9
  • 51
  • 87
2
votes
1 answer

use collection initializer with insert statement

I have the following method: public bool InsertUsername(string username) { string SQL = "Insert into [Users](Username, InsertDateTime) Values(@Username, datetime('now'));"; List pars = new List(); …
Max
  • 12,622
  • 16
  • 73
  • 101