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
17
votes
1 answer

Why does this nested object initializer throw a null reference exception?

The following testcase throws a null-reference exception, when it tries to assign Id to an object which is null, since the code is missing the "new R" before the object initializer. Why is this not caught by the compiler? Why is it allowed, in which…
17
votes
2 answers

Objective-C Multiple Initialisers

I have a simple question about creating multiple initialisers within an objective-c class. Basically I have a class that represents a single row in my database (users). I currently have an initialiser which initialises the class based upon the users…
Mick Walker
  • 3,862
  • 6
  • 47
  • 72
16
votes
7 answers

Is this a bug in the C# 4.0 compiler?

This code compiles successfully, but I think it should fail to compile. Also, when you run it you get a NullReferenceException. The missing code is the "new Bar" in the initialization of the Bar property. class Bar { public string Name { get;…
15
votes
3 answers

C# object initialization syntax in F#

Please note: this question is not the same as this question. I recently came across some C# syntax I hadn't previously encountered: Is there any way to do this in F#? class Two { public string Test { get; set; } } class One { public One() …
N_A
  • 19,799
  • 4
  • 52
  • 98
14
votes
4 answers

How to reinitialize an array in C (not C++)?

How can I reinitialize an array in C? Should I define it through a pointer: int *data[], or can I just code: int data[] = {};? For example, I have defined it like: int main(void) { int data[] = {44, 22, 1, 2, 3, 1000, 3}; Now, how do I…
user1131997
14
votes
1 answer

Code Contracts Vs. Object Initializers (.net 4.0)

At face value, it would seem that object initializers present a problem for .net 4.0 "code contracts", where normally the invariant should be established by the time the object constructor is finished. Presumably, however, object-initializers…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
11
votes
2 answers

Optimizing multiprocessing.Pool with expensive initialization

Here is a complete simple working example import multiprocessing as mp import time import random class Foo: def __init__(self): # some expensive set up function in the real code self.x = 2 print('initializing') def…
11
votes
5 answers

Can you Instantiate an Object Instance from JSON in .NET?

Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to…
Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166
10
votes
3 answers

Array of dynamic | ExpandoObject | with a compressed initialize syntax

Im trying to use DynamicObject in c#, and I needed an array of dynamic: var d = new dynamic[]; which works fine. EDIT : See ExpandoObject below. But I also like to fill that array with some data with this compressed initialize new syntax: var d =…
joeriks
  • 3,382
  • 8
  • 32
  • 42
10
votes
2 answers

Using __new__ to override __init__ in subclass

I'm interested in using the __new__ functionality to inject code into the __init__ function of subclasses. My understanding from the documentation is that python will call __init__ on the instance returned by __new__. However, my efforts to change…
uayebforever
  • 673
  • 8
  • 12
9
votes
4 answers

Why doesn't name exist in the current context of shorthand member initialisation?

I am using an object initializer for a st object: public class Container { public Container () { ContainedItem = new Item; } public Item ContainedItem { get; set; } } public class Item { public string Value { get; set; } } var…
Toby
  • 9,696
  • 16
  • 68
  • 132
9
votes
5 answers

Removing the work from __init__ to aid unit testing

The key to this question is aiding unit-testing. If I have a busy __init__ (i.e. __init__ that does complex initialization), I cannot simply instantiate an object of a class, but I need to mock/stub out all methods invoked on dependencies within the…
LavaScornedOven
  • 737
  • 1
  • 11
  • 23
8
votes
3 answers

c# constructors vs auto-properties and object initializers

I have used auto properties a lot but I have gone more and more away from that setting up classes with readonly backing fields initialized in the constructor. I remove all setters and only add the back if the property clearly need a setter. I find…
terjetyl
  • 9,497
  • 4
  • 54
  • 72
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
6 answers

What am I doing wrong with C# object initializers?

When i initialize an object using the new object initializers in C# I cannot use one of the properties within the class to perform a further action and I do not know why. My example code: Person person = new Person { Name = "David", Age = "29"…
dmce
  • 222
  • 2
  • 11
1
2
3
14 15