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
1
vote
2 answers

Do collection initializers relate to LINQ in anyway like object initializers?

I am reading about LINQ and seeing Collection Initialzers come up, but does it really directly relate to LINQ like Object Initializers do? List stringsNew = new List { "string 1", "string 2" };
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
1
vote
1 answer

Supporting collection initialization syntax for immutable collections - init modifiers for methods?

I need to implement an immutable collection which supports collection initialization syntax. The problem is in order to support collection initialization syntax, the collection must provide appropriate public Add() methods. However, if the…
1
vote
2 answers

Set inner object's member as outer object using the object initialization syntax in C#

Context I have a List of type Question. Class Question, in turn, contains a List. Class Answer has a member called public Question Question { get; set; } which stores the question of which the answer is for. I'm using the collection…
Amal K
  • 4,359
  • 2
  • 22
  • 44
1
vote
2 answers

Why is a collection initializer without `new` allowed inside an object initializer but not outside?

I noticed strange behaviour when initializing collection property. Consider: class X { public IList Ints { get; set; } } I can initialize Ints like that: var theObject = new X { Ints = { 12, 3, 4, 5, 6 } }; But I cannot do that: var x…
Pawel
  • 525
  • 3
  • 16
1
vote
1 answer

Use of C# 8 null-forgiving operator within C# 6 index initializers

When using an index initializer to fill a dictionary without recreating it before that, is there an option to specify the null-forgiving operator somewhere? Example: public class Program { public IDictionary? NullableDictionary {…
mxscho
  • 1,990
  • 2
  • 16
  • 27
1
vote
2 answers

Can't dynamically set array initializer length

I am using C# to hook into the FedEx API and I'm a bit stumped on how to modify some existing code to meet my needs. The snippet included is part of their canned code where they sample how to work with 1 commodity. However, when my code runs I…
user1134307
  • 218
  • 2
  • 5
  • 16
1
vote
2 answers

Initialization of list in class for availability from functions

I want initialize list with collection initializer values in class to make it available to use from separate functions: public Form1() { InitializeComponent(); } List list = new List() {"one", "two", "three"}; What is a…
user6841064
1
vote
1 answer

Object & Collection Initializers and Accessors

I'd like to start off with that I'm new to C# so accessors and Object Initializers are a completely new concept to me. That said, I think I have an okay handle on them except the following example is really confusing me: using System; using…
1
vote
0 answers

Will EF7 support collection initializers?

C# 6 introduced some cool additions to how objects and collections are initialized. For example this code is valid in C#6: void Main() { var ints = new[] { 1, 2, 3, 4, 5 }; var myClass = new MyClass { //…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
1
vote
5 answers

Undefined constructor error in a double bracket collection initializer

In the following code the getEntriesNotWorking method reports a compile-time error: public class DoubleBracketInitializerTest { class Container {} class Entry { Container container; public Entry(Container container) { …
Dariusz
  • 21,561
  • 9
  • 74
  • 114
0
votes
2 answers

Initializing a list of lambda expressions

I'm designing a state machine class and want to use lambda expressions to represent conditions to satisfy the state transition objects. When I create a new State Transition object, I also want to pass it a list of Conditions that it can use to…
Jason O
  • 753
  • 9
  • 28
0
votes
1 answer

IEnumerable Add method available only when using collection initializer

I'm planning to create an immutable class based on JObject. One of the requirement is to directly deserialize object onto my new custom class, just like we do with JObject. I've implemented GetEnumerator and Add method to enable collection…
Evan Park
  • 528
  • 1
  • 6
  • 20
0
votes
2 answers

System.Array does not support Add(), so how does collection initializer work?

In C# you can initialize an array like so: var example = new int[] { 1, 4, 3 }; As quoted in Custom Collection Initializers: The collection object to which a collection initializer is applied must be of a type that implements…
0
votes
3 answers

Does constructor have access to initialized properties

If I use an object initializer when instantiating an object does that objects constructor have access to the initialized properties? public class ExampleClass { public string proptery1 { get; set; } public string property2 { get; set; } …
bflemi3
  • 6,698
  • 20
  • 88
  • 155
-1
votes
2 answers

C# Why dictionary allows property setting though the property is internal set

I'm using fluent validation API for writing validations. I came across a internal set property on ValidationContext class. public class ValidationContext { public Dictionary RootContextData { get; internal set; } = new…
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60