Questions tagged [object-initializers]

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.

An example of object initializers is the Object and Collection Initializers (C# Programming Guide):

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements. The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).

215 questions
333
votes
26 answers

What should my Objective-C singleton look like?

My singleton accessor method is usually some variant of: static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance = [[self alloc] init]; } …
schwa
  • 11,962
  • 14
  • 43
  • 54
114
votes
3 answers

Object initialization syntax

I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3. I.e. given this: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } how do I write the following…
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
100
votes
8 answers

Best practice to implement a failable initializer in Swift

With the following code I try to define a simple model class and it's failable initializer, which takes a (json-) dictionary as parameter. The initializer should return nil if the user name is not defined in the original json. 1. Why doesn't the…
Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
66
votes
5 answers

Constructor vs Object Initializer Precedence in C#

I've been learning the object initializer in C# recently, but now I'm wondering how it works when it conflicts with the constructor. public class A { public bool foo { get; set; } public A() { foo = true; } public…
bbill
  • 2,264
  • 1
  • 22
  • 28
53
votes
5 answers

Initial capacity of collection types, e.g. Dictionary, List

Certain collection types in .Net have an optional "Initial Capacity" constructor parameter. For example: Dictionary something = new Dictionary(20); List anything = new List(50); I can't seem to find…
Neil N
  • 24,862
  • 16
  • 85
  • 145
41
votes
5 answers

How to debug object initializer code?

Is there a way to step by step debug the object initializer code in Visual Studio? Example: return new Veranstaltung() { ID = tblVeranstaltung.VeranstaltungsID, Titel = tblVeranstaltung.Titel, …
magnattic
  • 12,638
  • 13
  • 62
  • 115
39
votes
1 answer

Nested object initializer syntax

Resharper has just suggested the following refactoring to me: // Constructor initializes InitializedProperty but // the UninitializedSubproperty is uninitialized. var myInstance = new…
Rawling
  • 49,248
  • 7
  • 89
  • 127
37
votes
1 answer

Order of operations using Object Initializer Syntax

Does the order in which I set properties using the object initializer syntax get executed in the exact same order? For instance if I do this: var s = new Person { FirstName = "Micah", LastName = "Martin", …
Micah
  • 111,873
  • 86
  • 233
  • 325
32
votes
2 answers

Use object initializer - Resharper suggestion

I use ReSharper everyday, and today I asked myself why ReSharper suggests "Use object initializer" when I do this : MyClass myClass = new MyClass(); myClass.MyInt = 0; myClass.MyString = string.Empty; It gets replaced by : MyClass myClass = new…
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
31
votes
2 answers

Initializer syntax

I like the C# 3 initializer syntax and use it a lot, but today while looking in Reflector, the following came up: var binding = new WSHttpBinding { ReaderQuotas = { MaxArrayLength = 100000 }, MaxReceivedMessageSize = 10485760 }; At first I…
leppie
  • 115,091
  • 17
  • 196
  • 297
27
votes
3 answers

How to change the formatting of the "Use Object Initializer" refactoring in Resharper?

When I refactor the following line: Employee e = new Employee(); e.First = "Frank"; e.Last = "Rizzo"; using Resharper's "Use Object Initializer", I get the following: Employee e = new Employee { First = "Frank", …
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
26
votes
3 answers

Assigning events in object initializer

Why isn't it possible to assign events along with properties in object initializers in C#? It seems to be so natural to do so. var myObject = new MyClass() { Property = value, Event1 = actor, // or Event2 +=…
Max
  • 19,654
  • 13
  • 84
  • 122
21
votes
3 answers

c# object initializer complexity. best practice

I was too excited when object initializer appeared in C#. MyClass a = new MyClass(); a.Field1 = Value1; a.Field2 = Value2; can be rewritten shorter: MyClass a = new MyClass { Field1 = Value1, Field2 = Value2 } Object initializer code is more…
Andrew Florko
  • 7,672
  • 10
  • 60
  • 107
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
18
votes
5 answers

Can properties inside an object initializer reference each other?

Is it somehow possible for properties to reference each other during the creation of a dynamic object an anonymously-typed object (i.e. inside the object initializer)? My simplified example below needs to reuse the Age property without making a…
user3199179
  • 271
  • 3
  • 8
1
2 3
14 15