object-initializers

Nested object initializer syntax

流过昼夜 提交于 2019-11-26 20:45:37
Resharper has just suggested the following refactoring to me: // Constructor initializes InitializedProperty but // the UninitializedSubproperty is uninitialized. var myInstance = new MyClass(); myInstance.InitializedProperty.UninitializedSubproperty = new MyOtherClass(); // becomes var myInstance = new MyClass { InitializedProperty = { UninitializedSubproperty = new MyOtherClass() } }; I've never seen this kind of object initialization before. In particular I don't see how InitializedProperty = { UninitializedSubproperty = new MyOtherClass() } makes any sense - it's not assigning anything to

Assigning events in object initializer

旧城冷巷雨未停 提交于 2019-11-26 16:58:29
问题 Why isn't it possible to assign events along with properties in object initializers in C#? It seems to be so natural to do so. var myObject = new MyClass() { Property = value, Event1 = actor, // or Event2 += actor }; Or is there some trick that I don't know of? 回答1: As far the external contract is concerned, an event doesn't have a setter, only add and remove methods - subscribers can register and unregister from the event, and the publishing object decides when to invoke the callbacks by

Initializer syntax

喜夏-厌秋 提交于 2019-11-26 14:15:21
问题 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 }, MaxReceivedMessageSize = 10485760 }; At first I thought it was a mistake, but it does compile! Guess I am still learning new stuff all the time. :) From what I can tell, it sets the MaxArrayLength property of the ReaderQuotas property of the WSHttpBinding . Does this syntax create a new ReaderQuotas

Best practice to implement a failable initializer in Swift

一世执手 提交于 2019-11-26 12:05:58
With the following code I try to define a simple model class and it's failable initializer, which takes a (json-) dictionary as parameter. The initializer should return nil if the user name is not defined in the original json. 1. Why doesn't the code compile? The error message says: All stored properties of a class instance must be initialized before returning nil from an initializer. That doesn't make sense. Why should I initialize those properties when I plan to return nil ? 2. Is my approach the right one or would there be other ideas or common patterns to achieve my goal? class User:

Order of operations using Object Initializer Syntax

梦想的初衷 提交于 2019-11-26 08:34:59
问题 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 = \"Martin\", IsLoaded = true } will each property get set in the same order? 回答1: Yes. Apologies for getting interrupted (I have to actually do some work every so often). The spec doesn't explicitly say it, but it makes it pretty clear IMO in section 7.6.10.2: An object initializer consists of a

Nested object initializer syntax

白昼怎懂夜的黑 提交于 2019-11-26 07:45:00
问题 Resharper has just suggested the following refactoring to me: // Constructor initializes InitializedProperty but // the UninitializedSubproperty is uninitialized. var myInstance = new MyClass(); myInstance.InitializedProperty.UninitializedSubproperty = new MyOtherClass(); // becomes var myInstance = new MyClass { InitializedProperty = { UninitializedSubproperty = new MyOtherClass() } }; I\'ve never seen this kind of object initialization before. In particular I don\'t see how

Best practice to implement a failable initializer in Swift

笑着哭i 提交于 2019-11-26 02:50:33
问题 With the following code I try to define a simple model class and it\'s failable initializer, which takes a (json-) dictionary as parameter. The initializer should return nil if the user name is not defined in the original json. 1. Why doesn\'t the code compile? The error message says: All stored properties of a class instance must be initialized before returning nil from an initializer. That doesn\'t make sense. Why should I initialize those properties when I plan to return nil ? 2. Is my

What should my Objective-C singleton look like? [closed]

时间秒杀一切 提交于 2019-11-25 23:56:19
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not