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

generic class partial initializer

I have a lot of similar classes that I'd like to initialize with the following syntax: class A { b: number = 1 constructor(initializer?: Partial) { Object.assign(this, initializer) } } new A({b: 2}) I think that being able…
Yovar
  • 540
  • 1
  • 3
  • 16
2
votes
3 answers

initialize value to a public variable of type list of a class using the object initializer

I have a generic class which contain only one data member of type list. Now I want to add value to that list using the object initializer of that generic class from my main method. Here is my Generic Class class GenericStore { public List
Subhashis Pal
  • 139
  • 1
  • 11
2
votes
2 answers

Order of assignments in object-initializer when using nested objects

I have the following code creating an instance of Root using an object-initializer: var r = new Root { Person = new Person { Age = 20, Name = "Hans" } }; From Is there any benefit of using an Object Initializer? I know if we´d have only the inner…
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2
votes
2 answers

Collection initializer on Add method generic parameter?

Can I use a collection initializer on my class which have an Add method that takes a generic parameter? My class looks like this: public class FooCollection : IEnumerable> { private readonly IDictionary
Fred
  • 12,086
  • 7
  • 60
  • 83
2
votes
0 answers

Object initializers in C++/CLI

In C# I can do this: foo( new SomeClass() { SomeProperty = "value" } ); Now in C++/CLI I can equivalently do this: auto tmp = gcnew SomeClass(); tmp->SomeProperty="value"; foo (tmp); But is there a syntax for C++/CLI that is similar to C# object…
Kevin Holt
  • 775
  • 9
  • 15
2
votes
2 answers

anonymous object initialisation with single variable

I found have found such a line: var myObject = new MyClass { 42 }; And I would like to know if it is possible to perform such operation. Documentation says that ,,You must use an object initializer if you're defining an anonymous type" so this is…
user2999425
  • 125
  • 1
  • 11
2
votes
2 answers

How do you call class method from initialize method?

I'm trying to call a method in a class during its initialize method. Is this not allowed? I originally had the method outside of the class to try and use it as a global method. The current method is trying to return the created matrix and then the…
minnymauer
  • 374
  • 1
  • 4
  • 17
2
votes
2 answers

use "this" inside Object initializer

Suppose I have this code: List resultList = new List(); foreach (var item in collection) { resultList.Add(new MyObj { X = item.foo, Y = item.bar, Z = [I want it to be X + Y] }); } I can't use the…
Alonzzo2
  • 959
  • 10
  • 24
2
votes
2 answers

Are there object initializers for HTMLElements in TypeScript?

Instead of this code: let img = document.createElement("img"); img.src = "foo.png"; img.className = "bar"; img.id = "abc123"; I'd like to do something like this: let img = document.createElement("img") { src = "foo.png"; className = "bar"; …
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
2
votes
2 answers

Object initzializer/constructor self reference for lambda expression

I have a simple filter class looking like this: public class DateFilter { public DateTime Value { get; set; } public Func Match { get; set; } } Is it possible to initialize the Match function in the constructor or the…
lolsharp
  • 391
  • 4
  • 16
2
votes
1 answer

How do you create a SwiftyJSON dictionary out of optional AnyObjects?

I recently ran into a very time-consuming issue for a simple task of creating a JSON dictionary with optional objects. I'm using SwiftyJSON. If I create the following dictionary, I get thrown errors JSON type doesn't have an initializer that…
trixmasta
  • 233
  • 3
  • 14
2
votes
4 answers

Object initializers and Constructors

I am trying to use Object initializers to set the properties of a class and then access them within the constructor of the class. The problem is that the properties do not seem to be set until after the constructor runs. Am I doing something wrong.…
TampaRich
  • 793
  • 2
  • 9
  • 22
2
votes
1 answer

What should a C# object initializer for a Dictionary look like?

I am trying to declare a dictionary whose values are string arrays. How can I do this? I tried the following code (which does not work): Dictionary NewDic = new Dictionary { {"Key_0", {"Value_0.0",…
Gaduks
  • 671
  • 6
  • 13
2
votes
1 answer

Overriding initialize method into model in Rails

I'm trying to override the initialize method. See below class Restriction < ActiveRecord::Base RESTRICTION_TYPES = { less_than: "IND
2
votes
1 answer

Can I use object initializer outside construction of an object?

Given object initializers: Foo foo = new Foo{ Name = "Jhon", Value = 2, IsMale = true }; Can they be somehow used elsewhere?(outside object construction) So that insted of using: foo.Name = "Name"; foo.Value = 5; ... foo.DoSth(); To just use…
Bosak
  • 2,103
  • 4
  • 24
  • 43