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

Cannot Identify the property which throw exception when using Object initializer in C#

In the below two sample code I am trying to instantiate a class named Test by using C# normal method and Object initializer. DateTime? nullDate = null; //this value will come from somewhere else DateTime? notNullDate = DateTime.Now; var test = new…
Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79
7
votes
2 answers

Why am I allowed to modify properties which are readonly with object initializers?

I have this simple code: public static void Main(String[] args) { Data data = new Data { List = { "1", "2", "3", "4" } }; foreach (var str in data.List) Console.WriteLine(str); Console.ReadLine(); } public class Data { …
7
votes
3 answers

Object Initializer and Dynamically specifying properties

With an object initializer, is it possible to optionally include setting of property? For example: Request request = new Request { Property1 = something1, if(something) Property2 = someting2, …
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
7
votes
2 answers

Setting a private setter using an object initializer

Why is it possible to use an object initializer to set a private set auto property, when the initializer is called from within the class which owns the auto property? I have included two class as an example. public class MyClass { public string…
6
votes
2 answers

C# The order of fields initialization

I create objects by initializing the fields in this way: class Example { public int a; public int b; } var obj = new Example { a = stream.ReadInt(), b = stream.ReadInt() }; Is it always for field "a" to be initialized before field…
6
votes
3 answers

covariant object initializers?

say that I have an class that has a property that is a dictionary, using a object initializer I can use this syntax (which I think looks pretty clean): new MyClass() { Table = { {"test",true},{"test",false} } } however, outside of…
Homde
  • 4,246
  • 4
  • 34
  • 50
6
votes
2 answers

Conceptual reason of the 'A field initializer cannot reference the non-static field, method, or property' CS0236 Error

C# does not allow an instance field initializer to reference another field. For instance this code is not valid : class A { string s1 = ""; string s2 = s1; } because "s2" references "s1". But why this is not permitted ? My first thought was that…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
6
votes
2 answers

Populate array directly through object initializer

I have these 2 classes: class Customer { public string Name; public string City; public Order[] Orders; } class Order { public int Quantity; public Product Product; } And then in the…
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
5
votes
2 answers

Object context of 'this' within a jQuery event

Say I have the following defined in javascript: com.company.long.namespace = { actions: { add: { defaults: { url: 'myurl/submit', }, invoke: function () { var…
cweston
  • 11,297
  • 19
  • 82
  • 107
5
votes
4 answers

Is an object constructed if an initializer throws?

I was reading This Article over on Jag Reeghal's blog and It seemed to me that what he was suggesting was really not the same thing as using an object initializer. Then I realized that I didn't really know for sure. When an object is constructed,…
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
5
votes
1 answer

C# nested object initializer

C# 5.0 Language Specification 7.6.10.2 Object initializers states that A member initializer that specifies an object initializer after the equals sign is a nested object initializer, i.e. an initialization of an embedded object. Instead of…
user6338533
5
votes
4 answers

C# Object Initialiser - Reference to the new instance

Can I somehow get a reference to the instance I am creating using object initialiser var x = new TestClass { Id = 1, SomeProperty = SomeMethod(this) } "this" should point to the new TestClass…
Stephane
  • 1,042
  • 7
  • 18
5
votes
1 answer

How to implement a clean Custom Object Initializer for a Matrix class

I have a custom Matrix class that I would like to implement a custom object initializer similar to what a double[,] can use but can not seem to figure out how to implement it. Ideally I would like to have it look like this var m1 = new Matrix …
PlTaylor
  • 7,345
  • 11
  • 52
  • 94
5
votes
1 answer

Elasticsearch.NET NEST Object Initializer syntax for a highlight request

I've got: var result = _client.Search(new SearchRequest("blaindex", "blatype") { From = 0, Size = 100, Query = titleQuery || pdfQuery, Source = new SourceFilter …
notsoobvious
  • 175
  • 1
  • 9
5
votes
1 answer

Resharper - use object initializer refactor - how to retain parentheses on constructor call?

When I use Resharper to refactor my code to use an Object initializer, it reformats the code correctly as thus, the following code var response = new Response(); response.Value = "My value"; becomes var response = new Response { Value = "My…
Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129
1 2
3
14 15