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

Field initializer is in constructor in IL, but not when debugging in Visual Studio

In IL code,the field initialization is in constructor. Field initialization in Constructor But in VS2017 debug ,the field initialization is not in constructor, but in class. Field initialization in VS Debug Source Code: class A { public int id…
0
votes
3 answers

Java empty blocks inside a method different from other blocks?

I understand that a block defines a scope of a variable. And empty blocks inside a method are for setting scope. But why are empty blocks inside methods initialising variables as well unlike in blocks used with loops etc. class A{ public static…
user10435687
0
votes
1 answer

C# List initialization using a list instead of Initializing List

My issue concerns the syntax of object initialization syntax in C#, specifically the syntax for initialize a List Property. JSchema of the Newtonsoft .NET Schema library provide a Property named Enum that is a IList and i want use object…
msalafia
  • 2,703
  • 5
  • 23
  • 34
0
votes
2 answers

Curly Braces in C# Method

I'm currently programming in C# and found this snippet in one of the tutorials. What exactly do the curly braces in this method mean? Is it like a key value pair {id: 2}? weapon = new Weapon(new WeaponData() { Id = 12 });
lost9123193
  • 10,460
  • 26
  • 73
  • 113
0
votes
1 answer

Is it bad practice to initialize variables in class members?

If a variable's intended scope is truly local, it would only make sense to me to keep it that way. This is what I have always done. However I recently switched from vim to eclipse at work and eclipse is flagging my constructor if every member…
mreff555
  • 1,049
  • 1
  • 11
  • 21
0
votes
3 answers

How to create an extensible API, and still use object initializer syntax?

I have a class library that wraps the command line client for Mercurial. My intention is to implement support for all the built-in commands, but in addition to those, there's a ton of extensions out there. So I need to make my library extendable in…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0
votes
1 answer

Call a plain void function from member initialization list?

I have a funky problem here. I benchmark object creation with a few simple timers. My code-base is slowly moving away from being a prototype and more of a complete framework. This requires initializing some simple structs at a top level (out of…
scx
  • 3,221
  • 1
  • 19
  • 37
0
votes
1 answer

Swift Initializers with same argument types

I am not sure how this works. As you can see there are two suggestions with the same name and argument type. How it is decided which method is called? It's the method with an argument which is called, but what if what I wanted to be called is the…
Bogdan Pop
  • 318
  • 1
  • 3
  • 12
0
votes
1 answer

How to initialize class and override its parent in MQL?

I've two classes: Chart which extends Market. I'd like to initialize the Chart class, but providing the pointer to already existing parent class to save some memory (to avoid initialization of new instances if it can point to the same thing). Here…
kenorb
  • 155,785
  • 88
  • 678
  • 743
0
votes
1 answer

Initializing classes using Code First Entity Framework

I've got a small project and just after some advice on how to seed and initialize some data. The two main classes are Client and History. There is a one-to-one relationship between these. Within History in addition to properties there are also a…
user1447825
0
votes
0 answers

Linq sorts list in object initializer

Today I've faced one very strange behavior. After creating an object with Linq query and object initializer with setting property of List type the original collection and the collection that the object contains have different entries…
keymusicman
  • 1,281
  • 1
  • 10
  • 20
0
votes
1 answer

Using Object Initializers dynamically

I need to pass a list of integers to a stored procedure because Entity Framework takes too long to process the request. I'm using a User Defined Table Type to do this. I'm using EntityFrameworkExtras.EF6 and I've created a stored procedure and Table…
0
votes
1 answer

What is the best way to represent all countries in the world in Android?

As you see in the JSON below, I want to represent all the countries in the world. For Firebase de-serialization, I started creating classes like Germany.java, Italy.java, but then it hit me, I have to write 249 classes and in Android when it comes…
Tord Larsen
  • 2,670
  • 8
  • 33
  • 76
0
votes
1 answer

Use object that is being initialized as parameter

I stumbled upon the problem of not being able to use a windows method as a property inside the objects initializer: var window = new DialogWindow { DataContext = new YesNoDialogViewModel() { DialogMessage = confirmation.Content as…
Mafii
  • 7,227
  • 1
  • 35
  • 55
0
votes
1 answer

Why is my IQueryable LINQtoObject being treated as LINQtoSQL and throwing no supported translation to SQL

I have a LINQ dbml class that I am wrapping in a POCO. I have built overloaded constructors that take the DBML class and init. the wrapper objects properties based on the dbml object passed in. For example public class MyPerson{ public…
ctrlShiftBryan
  • 27,092
  • 26
  • 73
  • 78