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

What is the behavior when there are more initializers than array size?

I would like to know what happens when there are more initializers than array size, e.g. : int t[3] = { 1, 2, 3, 4 }; Of course, my compiler warns it. I expected undefined behavior, but I didn't find any clause about it in C11 standard. So, did I…
md5
  • 23,373
  • 3
  • 44
  • 93
3
votes
3 answers

How to reference another property value in a C# member initializer?

In VB, the following is a valid object initializer in which one member intializer references the value of another member that has previously been initialized. new MyObject() with {.Property1="x", .Property2 = .Property1 + "y"} If I try to do the…
Richard Collette
  • 5,462
  • 4
  • 53
  • 79
3
votes
3 answers

How does C# set a read-only property on anonymous object initialization

In C#, object initializers can set public non-read-only fields and properties. However, with anonymous types, the properties are read-only. So how does .NET set them on object initialization?
James McCormack
  • 9,217
  • 3
  • 47
  • 57
2
votes
2 answers

Invalid initializer member declarator when projecting into complex types

I was initializing the object in the code below using simple properties but then refactored elsewhere such that DispatchedDocumentDate became DispatchedPhase.DocumentDate. I did this because there is also sold and picked classes using precisely the…
rism
  • 11,932
  • 16
  • 76
  • 116
2
votes
6 answers

How to tell when object initializer is done

I have various derived objects that I would like the user to be able to use object initializers with. I have an "Initializing" property that I want to be true as those fields are being set and then I want the Initializing property to be set to…
Brandon Moore
  • 8,590
  • 15
  • 65
  • 120
2
votes
1 answer

Why does my array initializer not work unless I create a separate variable for it?

The following C++ code compiles with no errors or warnings, but for some reason when I print out the contents of foo.arr I just get junk values. It looks like the array member is not being properly initialized. template class C{ public: …
2
votes
1 answer

static initializer list for wrapper class

Is it possible to "pass" somehow a static initializer list at construction time to a container wrapper class that than in turn initializes its member? struct bar { bar(void * ptr): ptr(ptr) {} void * ptr; }; template struct…
ritter
  • 7,447
  • 7
  • 51
  • 84
2
votes
2 answers

Object instantiation with predefined properties

I am studying the mailkit library, I found just such a construction in one line in c# msg.Body = new TextPart("html") { Text = "html content" }; on Powershell I can do as many as three lines $TextPart =…
2
votes
2 answers

"In constructors and initializers, only property or field parameter bindings are supported" when using object initializer syntax

I'm getting a very odd problem in Entity Framework query that I literally spent hours on. When a query is executed, I get an exception: In constructors and initializers, only property or field parameter bindings are supported in LINQ to…
2
votes
1 answer

POD default initialisation if static and constructor if automatic?

In the example below, Y and X give a warning "variable has static storage duration and non-POD type" (pclint, Autosar A3-3-2). struct Y {int t; Y() {t = 0;}}; class X {private: int t; public: X() {t = 0;}}; struct Z {int t;}; X x; // warning:…
Ernie Mur
  • 481
  • 3
  • 18
2
votes
1 answer

Configure code cleanup profile to delete object initializer constructor parentheses

How can I configure my code cleanup profile in Visual Studio 2019 to change this piece of code new List() { key } to this one new List { key } when I am running code cleanup?
2
votes
1 answer

Hashtable key with point

How do I issue a hash table key with a point character, such as FlatAppearance.BorderSize, so that it is correct? $Button = [System.Windows.Forms.Button] @{ # This is not a valid entry FlatAppearance.BorderSize = 0 } $Button =…
2
votes
4 answers

Is this code setting values via accessors soon after object creation

var dlg = new Microsoft.Win32.OpenFileDialog { Title = "Select configuration", DefaultExt = ".xml", Filter = "XML-file (.xml)|*.xml", CheckFileExists = true }; I got the above piece of got from this post. Is the part inside the…
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
2
votes
3 answers

Difference on initializing object inside class declaration v/s constructor

I was going through object initialization and constructor initialization for my object, but couldn't get exact reply to my question. What is the difference between Case1 and Case2 here; Case 1: namespace ConsoleApplication2 { class MyBuilder …
OmGanesh
  • 952
  • 1
  • 12
  • 24
2
votes
4 answers

Ninject with Object Initializers and LINQ

I'm new to Ninject so what I'm trying may not even be possible but I wanted to ask. I free-handed the below so there may be typos. Let's say I have an interface: public interface IPerson { string FirstName { get; set; } string LastName {…