object-initializers

What am I doing wrong with C# object initializers?

喜欢而已 提交于 2021-02-04 15:22:07
问题 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 person = new Person { Name = "David", Age = "29" }; Within the Person Class, x will equal 0 (default): public Person() { int x = Age; // x remains 0 - edit age should be Age. This was a typo } However person.Age does equal 29. I am sure this is normal, but I would like to understand why. 回答1: The

Object initializer for readonly properties in c#

跟風遠走 提交于 2020-07-10 07:10:24
问题 If you have the class: class Foo { Bar Bar { get; } = new Bar(); } class Bar { string Prop {get; set; } } You can use a object initialise like: var foo = new Foo { Bar = { Prop = "Hello World!" } } If you have a class class Foo2 { ICollection<Bar> Bars { get; } = new List<Bar>(); } You can write var foo = new Foo2 { Bars = { new Bar { Prop = "Hello" }, new Bar { Prop = "World" } } } but, I would like to write something like var items = new [] {"Hello", "World"}; var foo = new Foo2 { Bars = {

How can I use Console.Write in object initializer?

痴心易碎 提交于 2020-07-03 06:46:48
问题 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 = Console.ReadLine(), LastName = Console.ReadLine(), ID = Convert.ToInt32(Console.ReadLine()), Hoghoogh = Convert.ToDouble(Console.ReadLine()) }; 回答1: You can't because Console.Write is not an accessible property or field of Karmand . You can only set values of class properties and fields in object initializers.

Setting a private setter using an object initializer

感情迁移 提交于 2020-02-28 06:25:48
问题 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 myName { get; private set; } public string myId { get; set; } public static MyClass GetSampleObject() { MyClass mc = new MyClass { myName = "Whatever", // <- works myId = "1234" }; return mc; } } public class MyOtherClass { public static MyClass

Populate array directly through object initializer [closed]

醉酒当歌 提交于 2020-02-23 10:27:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . 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 Main I do the following: Customer cust = new Customer { Name = "some name", City = "some city", Orders = { new Order { Quantity =

Populate array directly through object initializer [closed]

早过忘川 提交于 2020-02-23 10:27:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . 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 Main I do the following: Customer cust = new Customer { Name = "some name", City = "some city", Orders = { new Order { Quantity =

covariant object initializers?

帅比萌擦擦* 提交于 2020-01-02 03:36:08
问题 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 = { {"test",true},{"test",false} } } however, outside of the initializer I can't do this: this.Table = { {"test",true},{"test",false} }; Why are initializers a special case? I'd hazard a guess that it has something to do with LINQ requirements, covariance or whatnot but it feels a little incongruent not being

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

痴心易碎 提交于 2020-01-01 07:39:11
问题 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 {