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

C# object initializer wanting to use wrong Add method

I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable, IStringIndexable, IDictionary, ICollection>, IEnumerable>, …
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
5
votes
1 answer

What is the name of this C# syntax?

In C#, you can do something like this: SomeClass someClass = new SomeClass () { SomeProperty = someValue }; What is this syntax called?
Matthew
  • 28,056
  • 26
  • 104
  • 170
5
votes
1 answer

Why does using an Object Initializer keep an object alive?

I recently came across this SO article and tweaked it for my scenario which follows: using System; using System.Collections.Generic; namespace ConsoleApplication18 { class Program { static void Main(string[] args) { …
Jim
  • 4,910
  • 4
  • 32
  • 50
4
votes
3 answers

Object initializers in C# cause compile-time error

When compiling some C# code, I get the error: A new expression requires () or [] after type My code is as follows: request.AddExtension(new ClaimsRequest { Country = DemandLevel.Request, Email = DemandLevel.Request, Gender…
Adeel Aslam
  • 1,285
  • 10
  • 36
  • 69
4
votes
4 answers

How to write a Custom DynamicObject class that supports object initializers

In the documentation for DynamicObject, there is an example of a DynamicDictionary that allows you to work with a dictionary as if it's a class with properties. Here is the class (modified slightly for brevity): public class DynamicDictionary :…
devuxer
  • 41,681
  • 47
  • 180
  • 292
4
votes
2 answers

Object initializers in a LINQ query - is it possible to reuse calculated data?

I'm using a linq query which looks (after some simplification) something like the following: List listUserExams = GetUserExams(); var examData = from userExam in listUserExams group by userExam.ExamID into groupExams select new…
Kuzco
  • 315
  • 1
  • 6
  • 20
4
votes
2 answers

Can I safely use object initializers inside using statements?

I'm wondering if the use of object initializers inside using statements somehow prevents the correct disposal of the resource declared inside them, e.g. using (Disposable resource = new Disposable() { Property = property }) { // ... } I've read…
StackLloyd
  • 409
  • 2
  • 9
4
votes
2 answers

How can I use Console.Write in object initializer?

When I use Console.Write in object initializer I get this error Error CS0747 Invalid initializer member declarator person[i] = new Karmand() { Console.Write("first name:"), FirstName =…
4
votes
5 answers

C#: Object having two constructors: how to limit which properties are set together?

Say you have a Price object that accepts either an (int quantity, decimal price) or a string containing "4/$3.99". Is there a way to limit which properties can be set together? Feel free to correct me in my logic below. The Test: A and B are…
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
4
votes
3 answers

Is it possible to use Object Initializers on a bool?

Is it possible to do the following (e.g. initialize bool array and set all elements to true) in one line using object initializers? int weeks = 5; bool[] weekSelected = new bool[weeks]; for (int i = 0; i < weeks; i++) { weekSelected[i] =…
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
4
votes
3 answers

C# 3.0 Object Initialation - Is there notification that the object is being initialized?

We have several domain objects which need to support both read-only and read-write modes; they currently have a bool Locked property for this--when Locked attempts to alter properties on the object result in an InvalidOperationException. The…
STW
  • 44,917
  • 17
  • 105
  • 161
4
votes
2 answers

C# Object Initializer : Set Property from another one

I have the following object where in my constructor I add a new Guid as the Id. public class MyObject { public MyObject() { Id = Guid.NewGuid().ToString(); } public String Id { get; set; } public String Test { get; set; } } I want…
danbord
  • 3,605
  • 3
  • 33
  • 49
4
votes
1 answer

Object Initialization Not Working for Me

What am I missing here? I expected the following to work just fine: public class ProposalFileInfo { public int FileId { get; set; } public bool IsSupportDocument { get; set; } } // ... var attachments = new…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
4
votes
3 answers

Accessing properties from object initializer

I have the following Person class class Person { public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } } public…
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
3
votes
1 answer

Inline Definition and Declaration

Ok, im sure im mis-wording the concept but here it is anyways. I know in CSharp you can do el.AppendChild(new UISize(file, "TSize") { CX = 95, CY = 20 }); which declares a temporary bucket variable and then assigns the associate property values to…
GoldBishop
  • 2,820
  • 4
  • 47
  • 82