object-initializers Questions

5

Solved

The key to this question is aiding unit-testing. If I have a busy __init__ (i.e. __init__ that does complex initialization), I cannot simply instantiate an object of a class, but I need to mock/stu...
Larena asked 20/9, 2012 at 13:1

3

Solved

I have used auto properties a lot but I have gone more and more away from that setting up classes with readonly backing fields initialized in the constructor. I remove all setters and only add the ...
Trichinize asked 3/9, 2010 at 23:2

2

Solved

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 a...

3

Solved

Im trying to use DynamicObject in c#, and I needed an array of dynamic: var d = new dynamic[]; which works fine. EDIT : See ExpandoObject below. But I also like to fill that array with some d...
Doorstone asked 26/1, 2011 at 14:39

3

Solved

Can an attribute in C# be used with a collection initializer? For example, I'd like to do something like the following: [DictionaryAttribute(){{"Key", "Value"}, {"Key", "Value"}}] public class Fo...

3

Solved

Is is possible to combine a List initializer and object initializer at the same time? Given the following class definition: class MyList : List<int> { public string Text { get; set; } } //...
Platitudinize asked 6/6, 2011 at 15:16

3

say that I have an class that has a property that is a dictionary<string,bool>, using a object initializer I can use this syntax (which I think looks pretty clean): new MyClass() { Table = ...
Schaab asked 23/1, 2011 at 12:52

2

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...
Housemother asked 26/11, 2010 at 14:5

7

This code compiles successfully, but I think it should fail to compile. Also, when you run it you get a NullReferenceException. The missing code is the "new Bar" in the initialization of the Bar pr...
Interlaminate asked 1/10, 2010 at 14:19

4

Solved

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 TestClas...
Comeau asked 25/8, 2010 at 9:51

3

Solved

I was too excited when object initializer appeared in C#. MyClass a = new MyClass(); a.Field1 = Value1; a.Field2 = Value2; can be rewritten shorter: MyClass a = new MyClass { Field1 = Value1, ...
Dilative asked 18/6, 2010 at 7:43

1

Solved

At face value, it would seem that object initializers present a problem for .net 4.0 "code contracts", where normally the invariant should be established by the time the object constructor is finis...

3

Solved

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, ...
Honeysuckle asked 10/2, 2010 at 2:48

3

Solved

I have the following class hierarchy: public class Row : ICloneable, IComparable, IEquatable<Row>, IStringIndexable, IDictionary<string, string>, ICollection<KeyValuePair<strin...

2

Solved

I have a simple question about creating multiple initialisers within an objective-c class. Basically I have a class that represents a single row in my database (users). I currently have an initiali...
Melosa asked 8/1, 2010 at 10:32

2

Solved

I like the C# 3 initializer syntax and use it a lot, but today while looking in Reflector, the following came up: var binding = new WSHttpBinding { ReaderQuotas = { MaxArrayLength = 100000 }, Ma...
Julissa asked 7/1, 2010 at 12:17

1

Solved

In C#, you can do something like this: SomeClass someClass = new SomeClass () { SomeProperty = someValue }; What is this syntax called?
Boatsman asked 30/12, 2009 at 3:48

6

Solved

When i initialize an object using the new object initializers in C# I cannot use one of the properties within the class to perform a further action and I do not know why. My example code: Person ...
Clements asked 17/2, 2009 at 22:3

1

Solved

Does the order in which I set properties using the object initializer syntax get executed in the exact same order? For instance if I do this: var s = new Person { FirstName = "Micah", LastName =...
Confabulation asked 30/1, 2009 at 14:14

© 2022 - 2024 — McMap. All rights reserved.