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

Object initialization syntax on get-only field [HttpsRequestMessage.Header]

While searching around for how to work with HTTP request headers, i've seen a ton of examples use this construct to initialize the HttpRequestMessage: var request = new HttpRequestMessage() { Headers = { {…
3
votes
1 answer

compiler accepts almost-object-initializer that throws NullReferenceException

Possible Duplicate: Initializer syntax Short code sample to demonstrate (VS2010 SP1, 64-bit Win7): class A { public string Name { get; set; } } class B { public A a { get; set; } } // OK A a = new A { Name = "foo" }; // Using collection…
anton.burger
  • 5,637
  • 32
  • 48
3
votes
2 answers

C# object initializer will initialize read only properties, but for non-primitive types only

In the following test code I do not understand why the first line of TestMethod is legal, but the remaining two lines are not: public class Bar { public string Prop { get; set; } } public class Foo { public int Primitive { get; } = 0; …
MikeBeaton
  • 3,314
  • 4
  • 36
  • 45
3
votes
1 answer

How to initialize a structure object containing a mix of structure and simple elements?

Using VB.NET 2017, I declared a structure which has two members, one of them is a structure and the other is a string, as follows: Structure InpFile Dim name As String Dim reader As StreamReader Dim lineIn As String Dim lineNum As…
ysap
  • 7,723
  • 7
  • 59
  • 122
3
votes
5 answers

out parameters in object initializer during anonymous instantiation

If I have an object containing a public int property (public accessors), how can I parse a string to int when initializing this property at instantiation ? // Given initialized DataTable table; // Given public int IntProperty {get; set;} in public…
FooBar
  • 132
  • 1
  • 9
3
votes
2 answers

Why does this wrong object initialisation with curly braces even compile?

Whille creating some dummy data for a collection for a WPF/MVVM project, I produced the following wrong code, which compiles fine, but throws an exception at runtime. There's a nested structure of data objects, which I wrongly instantiate with only…
surface
  • 93
  • 6
3
votes
2 answers

Object initializer syntax confusion. Using parentheses in initializer?

So I was looking at the documentation to understand how to use an object initializer, be it for anonymous types or not. The only thing that I would like to find out is why(and if it matters) is there a difference in the example. It goes like this:…
knee pain
  • 600
  • 3
  • 19
3
votes
2 answers

Property initialization before constructor execution

I am using an object initializer to create an object with a Position property like this: var control = new HtmlTextbox(browser) { Position = position; }; As I know it's the same as: var control = new HtmlTextbox(browser); control.Position =…
yolo sora
  • 434
  • 5
  • 17
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
3
votes
2 answers

CodeDom and collection initializers

Is there a way to generate a dictionary initializer using the C# CodeDom? Are those supported at all? I would like to have: private IDictionary map = new Dictionary { { "Name", "Value" }, ... };
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
3
votes
0 answers

Have to initialize an object with nested properties and child properties in c#

Have to do recursive loop to find all the nested properties from a class till the last property and initialize them with a default value, if string then update with string.empty, if Enum then update with index[0], if int then update with 0, if…
rcube9
  • 41
  • 6
3
votes
1 answer

Initialization of get-only property in owner-object initializer

last time I came across a construct in C# Object Initializer: having: public class Class1 { public Class2 GetterOnlyProperty { get; private set; } public Class1() { this.GetterOnlyProperty = new Class2(); } } the…
Wojtek
  • 135
  • 1
  • 1
  • 9
3
votes
5 answers

Parenthesis with object initializers

In C#, I can have a class such as: class MyClass { public void DoStuff(); } I can initialize this class like: MyClass mc = new MyClass(); Or: MyClass mc = new MyClass { }; If I don't need the parenthesis, and there are no properties to be set…
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
3
votes
2 answers

Object Initializers, Non-Public Children, and Nested Initialization

I was looking through my codebase and found a line of code that R# had helpfully refactored for me. Here's a representative sample: public class A { public B Target { get; private set; } public object E { get; set; } public A() { …
eouw0o83hf
  • 9,438
  • 5
  • 53
  • 75
3
votes
2 answers

Are class initializers possible in C#?

In C# you have object initializers to initialize fields of objects at creation time without using a constructor. Now I'm wondering if there is an equivalent to classes which means that you can 'initialize' properties of classes when defining…
Bastian
  • 4,638
  • 6
  • 36
  • 55