object-initializers

Why does this nested object initializer throw a null reference exception?

梦想与她 提交于 2020-01-01 07:39:09
问题 The following testcase throws a null-reference exception, when it tries to assign Id to an object which is null, since the code is missing the "new R" before the object initializer. Why is this not caught by the compiler? Why is it allowed, in which use-cases would this be a meaningful construct? [TestClass] public class ThrowAway { public class H { public int Id { get; set; } } public class R { public H Header { get; set; } } [TestMethod] public void ThrowsException() { var request = new R {

C# Object Initializer : Set Property from another one

佐手、 提交于 2019-12-31 00:45:49
问题 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 to do something like that in an object initializer : var obj = new MyObject { Test = Id; // Get new GUID created in constructor } Is it possible? 回答1: No, you can't do that. You'd have to just set it in a separate statement: var obj = new MyObject(); obj.Test = obj

Can you Instantiate an Object Instance from JSON in .NET?

爷,独闯天下 提交于 2019-12-30 04:31:11
问题 Since Object Initializers are very similar to JSON, and now there are Anonymous Types in .NET. It would be cool to be able to take a string, such as JSON, and create an Anonymous Object that represents the JSON string. Use Object Initializers to create an Anonymous Type: var person = new { FirstName = "Chris", LastName = "Johnson" }; It would be awesome if you could pass in a string representation of the Object Initializer code (preferably something like JSON) to create an instance of an

Object initialization syntax

蓝咒 提交于 2019-12-28 08:07:30
问题 I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3. I.e. given this: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } how do I write the following in F#: var p = new Person { Name = "John", BirthDate = DateTime.Now }; 回答1: You can do it like this: let p = new Person (Name = "John", BirthDate = DateTime.Now) 回答2: the answer from CMS is definitely correct. Here is just one addition that may be also

Object initialization syntax

非 Y 不嫁゛ 提交于 2019-12-28 08:07:08
问题 I'm just starting out with F# and I can't find the syntax to do object initialization like in C# 3. I.e. given this: public class Person { public DateTime BirthDate { get; set; } public string Name { get; set; } } how do I write the following in F#: var p = new Person { Name = "John", BirthDate = DateTime.Now }; 回答1: You can do it like this: let p = new Person (Name = "John", BirthDate = DateTime.Now) 回答2: the answer from CMS is definitely correct. Here is just one addition that may be also

Is an object constructed if an initializer throws?

末鹿安然 提交于 2019-12-23 10:06:44
问题 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, with object initializers, and one of those intitializers throws (maybe a Null Reference exception)... is the object actually constructed? Is this basically like an exception being thrown in the constructor? Or is the object fully constructed, and

Is an object constructed if an initializer throws?

不打扰是莪最后的温柔 提交于 2019-12-23 10:06:10
问题 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, with object initializers, and one of those intitializers throws (maybe a Null Reference exception)... is the object actually constructed? Is this basically like an exception being thrown in the constructor? Or is the object fully constructed, and

Objective-C Multiple Initialisers

允我心安 提交于 2019-12-22 01:39:52
问题 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 initialiser which initialises the class based upon the users UserID (which is also the primary key within the database), when passed the UserID the class will them connect to a webservice parse the results and return an object initialised to the corresponding row in the database. Within this database are a

C# object initialization syntax in F#

你离开我真会死。 提交于 2019-12-19 12:38:11
问题 Please note: this question is not the same as this question. I recently came across some C# syntax I hadn't previously encountered: Is there any way to do this in F#? class Two { public string Test { get; set; } } class One { public One() { TwoProperty = new Two(); } public Two TwoProperty { get; private set; } } var test = new One(){ TwoProperty = { Test = "Test String" }}; (note the initialization of TwoProperty in the initializer when the setter is private - it is setting a property on the

Can properties inside an object initializer reference each other?

落爺英雄遲暮 提交于 2019-12-18 04:37:10
问题 Is it somehow possible for properties to reference each other during the creation of a dynamic object an anonymously-typed object (i.e. inside the object initializer)? My simplified example below needs to reuse the Age property without making a second heavy call to GetAgeFromSomewhere() . Of course it doesn't work. Any suggestion on how to accomplish this? var profile = new { Age = GetAgeFromSomewhere(id), IsLegal = (Age>18) }; Is something like this possible or not possible with dynamic