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
51
votes
3 answers

What benefits does dictionary initializers add over collection initializers?

In a recent past there has been a lot of talk about whats new in C# 6.0 One of the most talked about feature is using Dictionary initializers in C# 6.0 But wait we have been using collection initializers to initialize the collections and can very…
Pankaj
  • 1,446
  • 4
  • 19
  • 31
36
votes
3 answers

Initialize list with both a single object and another list of objects

I want to initialize a list with an object and a list of objects in that specific order. Currently, I am doing: List list = new List(); list.Add(object1); // object1 is type MyObject list.AddRange(listOfObjects); // listOfObjects…
Joe W
  • 1,789
  • 3
  • 28
  • 42
30
votes
2 answers

What does assignment to a bracketed expression mean in C#?

I'm reading Avalonia source code and I came across this sentence: return new MenuFlyoutPresenter { [!ItemsControl.ItemsProperty] = this[!ItemsProperty], [!ItemsControl.ItemTemplateProperty] = this[!ItemTemplateProperty] }; I've never seen a…
20
votes
1 answer

What is the difference between these two variations of collection initialiser expressions?

I've been using C# for a while, but recently noticed that the behaviour of one of my unit tests changed depending on which variation of collection initialiser expression I used: var object = new Class { SomeCollection = new List { 1, 2, 3 }…
Tagc
  • 8,736
  • 7
  • 61
  • 114
19
votes
3 answers

Combining List initializer and object initializer

Is is possible to combine a List initializer and object initializer at the same time? Given the following class definition: class MyList : List { public string Text { get; set; } } // we can do this var obj1 = new MyList() { Text="Hello"…
codymanix
  • 28,510
  • 21
  • 92
  • 151
19
votes
4 answers

C# 6.0's new Dictionary Initializer - Clarification

I've read that : The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object But looking at : var Dic = new Dictionary{ {"x",3}, {"y",7} }; vs. var Dic =…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
15
votes
2 answers

Why are collection initializers on re-assignments not allowed?

I always thought it worked fine both ways. Then did this test and realized it's not allowed on re-assignments: int[] a = {0, 2, 4, 6, 8}; works fine but not: int [ ] a; a = { 0, 2, 4, 6, 8 }; Any technical reason for this? I thought I would ask…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
11
votes
5 answers

Using collection initializer syntax on custom types?

I have a large static list which is basically a lookup table, so I initialise the table in code. private class MyClass { private class LookupItem { public int Param1 { get; set; } public int Param2 { get; set; } …
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
10
votes
2 answers

Anonymous collection initializer for a dictionary

Is it possible to implicitly declare next Dictionary: { urlA, new { Text = "TextA", Url = "UrlA" } }, { urlB, new { Text = "TextB", Url = "UrlB" } } so I could use it this way: foreach (var k in dic) { k.Key.Text =…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
9
votes
4 answers

Initialize elements with brackets like Lists in c#

I was thinking about arrays and lists and wondering if and how classes can get an implementation to be initializable like them. Let's take this class as basis: class TestClass { private List Numbers = new List (); // Insert code…
Battle
  • 786
  • 10
  • 17
7
votes
3 answers

Can I use a collection initializer for an Attribute?

Can an attribute in C# be used with a collection initializer? For example, I'd like to do something like the following: [DictionaryAttribute(){{"Key", "Value"}, {"Key", "Value"}}] public class Foo { ... } I know attributes can have named…
7
votes
1 answer

C# List or Collection initializer using foreach loop

Is it possible in C# to do something similar to the following? var tigerlist = new List(){ Tail = 10, Teeth = 20 }; var tigers_to_cats_approximation = new List() { foreach (var tiger in tigerlist) { …
Watson
  • 1,385
  • 1
  • 15
  • 36
7
votes
1 answer

Using collection initializer on my own class

I am trying to add collection initializing to my class. I read about the initializers here: https://msdn.microsoft.com/en-us/library/bb384062.aspx#Anchor_2 I'll quote the important part that puzzles me: Collection initializers let you specify one…
Inrego
  • 1,524
  • 1
  • 15
  • 25
7
votes
2 answers

Why am I allowed to modify properties which are readonly with object initializers?

I have this simple code: public static void Main(String[] args) { Data data = new Data { List = { "1", "2", "3", "4" } }; foreach (var str in data.List) Console.WriteLine(str); Console.ReadLine(); } public class Data { …
7
votes
1 answer

Using TextBoxFor to set HTML attributes that have a namespace prefix

I am converting some existing HTML to work with ASP.NET MVC, and have some forms containing input fields that have additional attributes (that, for now, I need to preserve) that contain a namespace prefix:
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
1
2 3 4