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
1
vote
2 answers

Can't dynamically set array initializer length

I am using C# to hook into the FedEx API and I'm a bit stumped on how to modify some existing code to meet my needs. The snippet included is part of their canned code where they sample how to work with 1 commodity. However, when my code runs I…
user1134307
  • 218
  • 2
  • 5
  • 16
1
vote
0 answers

How to start rails server without database.yml and add DB later in initializers?

I need to do a HTTP call for getting the credentials of the actual DB to use. So I've added code to get the credentials and updating the active record connection in an initializer file but whenever I start rails server it is looking for development…
C LOKESH REDDY
  • 230
  • 3
  • 9
1
vote
2 answers

Object initializer for readonly properties in c#

If you have the class: class Foo { Bar Bar { get; } = new Bar(); } class Bar { string Prop {get; set; } } You can use a object initialise like: var foo = new Foo { Bar = { Prop = "Hello World!" } } If you have a class class Foo2…
Stijn Van Antwerpen
  • 1,840
  • 17
  • 42
1
vote
0 answers

Can I use Object Initialiser syntax for base constructor somehow?

You can use this to quickly instantiate class' properties with values you'd like: Form myform = new Form { Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = "Hello world!", StartPosition =…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
1
vote
2 answers

How to construct an instance of Dictionary>

I am trying to code in C#, but I find that I can't construct an instance in this way like C++: Dictionary> FirSet = new Dictionary>() { { "a", {"ab", "abc"} }, { "b", {"bc", "bcd"} } …
Bowen Peng
  • 1,635
  • 4
  • 21
  • 39
1
vote
1 answer

Using Swift object class in Objective-C

Hello I have been looking at different questions regarding this and none of the solution seem to work for me. I have an object which I am trying to use in objective C, however I keep getting the 'No visible interface for object' when trying to…
paul590
  • 1,385
  • 1
  • 22
  • 43
1
vote
2 answers

Why this confusing syntax exists?

I've just read this question. If we have property of dictionary type: public class Test { public Dictionary Dictionary { get; set; } = new Dictionary { {"1", "1" }, {"2", "2" }, }; } Then…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
1
vote
5 answers

Why I cannot use Object Initializers in ASP.NET 2.0?

Why I can use Object Initializers in Visual Studio 2008 Windows projects, etc targeted to .NET 2.0 but cannot - in ASP.NET projects targeted to .NET 2.0 ? I understand that this is C# 3.0 features, but don't - why this possible to use in .NET 2.0…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1
vote
1 answer

VB.NET: Using an iterator, ObjArray.Select(Function(a) a.Property), to drive a parameterized constructor that creates a different object array

Suppose I have the following class declaration: Public Class MyObjectR Private mStr As String Public Sub New(ByVal _Var1 As String) mStr = _Var1 End Sub Public Property MyProperty As String …
jonk
  • 225
  • 4
  • 11
1
vote
1 answer

Object & Collection Initializers and Accessors

I'd like to start off with that I'm new to C# so accessors and Object Initializers are a completely new concept to me. That said, I think I have an okay handle on them except the following example is really confusing me: using System; using…
1
vote
1 answer

C# Class without explicit constructor and read only property

Can someone explain me how this code works ? public class Person { readonly List _children = new List(); public IList Children { get { return _children; } } public string Name { get; set;…
JaYo
  • 57
  • 6
1
vote
1 answer

Interface / IDisposable with Object Initializers and Properties

as a follow up to the question answered at How do I structure a class or a function/method (or interface) so that as part of a using, I can pass values in the {} brackets?, how do I utilized object Initializers and Properties with an Interface…
Kevin Scheidt
  • 95
  • 3
  • 10
1
vote
2 answers

Default constructor is getting called on a const reference member despite non default constructor arguments

Here is some basic C++ outline of code: #include #include #include using namespace std; class M { public: M() = default; ~M() { cout << "Called ~M" << endl; } }; class A { public: A(int z)…
crogg01
  • 2,446
  • 15
  • 35
1
vote
3 answers

Replacing memset() on classes in a C++ codebase

I've inherited a C++98 codebase which has two major uses of memset() on C++ classes, with macros expanded for clarity: // pattern #1: Obj o; memset(&o, 0, sizeof(o)); // pattern #2: // (elsewhere: Obj *o;) memset(something->o, 0,…
Catherine
  • 22,492
  • 3
  • 32
  • 47
1
vote
0 answers

Is there a reference for "Partial Object Assignment" inside the Object Initializer Syntax in C# ( Prop1 = { Prop2 = X })?

Given the following 2 classes (VS 2015 + .Net 4.5) with fully mutable properties: class Wrapped { public int A {get; set;} public int B {get; set;} } class Wrapper { public int X {get; set;} public Wrapped Y {get; set;} public…
StuartLC
  • 104,537
  • 17
  • 209
  • 285