object-initializers

Can properties inside an object initializer reference each other?

烂漫一生 提交于 2019-11-29 09:34:40
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 objects anonymously-typed object initializers ? Ron Beyer Unfortunately it's not possible, even with

Combining List initializer and object initializer

☆樱花仙子☆ 提交于 2019-11-29 09:17:39
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; } } // we can do this var obj1 = new MyList() { Text="Hello" }; // we can also do that var obj2 = new MyList() { 1, 2, 3 }; // but this one doesn't compile //var obj3 = new MyList() { Text="Hello", 1, 2, 3 }; Is this by design or is it just a bug or missing feature of the c# compiler? No, looking at the definitions from section 7.6.10 of the C# spec, an object-or-collection-initializer expression is either an

C# Object Initialiser - Reference to the new instance

点点圈 提交于 2019-11-28 14:07:40
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 instance I'm creating. But it obviously refers the the instance of the class in which this code resides. I'm not asking if this is a good way to do this. I'm aware that I can do this like this: var x = new TestClass {Id= x}; x.SomeProperty = SomeMethod(this); I have a complicated scenario, in which a reference to the new instance in the object initialiser would make life easier. Is this possible in any

Object initialization syntax

旧巷老猫 提交于 2019-11-28 04:01:58
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 }; CMS You can do it like this: let p = new Person (Name = "John", BirthDate = DateTime.Now) the answer from CMS is definitely correct. Here is just one addition that may be also helpful. In F#, you often want to write the type just using immutable properties. When using the "object

Combining List initializer and object initializer

邮差的信 提交于 2019-11-28 02:40:19
问题 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; } } // we can do this var obj1 = new MyList() { Text="Hello" }; // we can also do that var obj2 = new MyList() { 1, 2, 3 }; // but this one doesn't compile //var obj3 = new MyList() { Text="Hello", 1, 2, 3 }; Is this by design or is it just a bug or missing feature of the c# compiler? 回答1: No, looking at the

Assigning events in object initializer

烈酒焚心 提交于 2019-11-27 14:59:11
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? 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 'raising' the event. Consequently, the idea of "assigning an event", in general, is meaningless. However, when

Initializer syntax

前提是你 提交于 2019-11-27 08:34:52
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 object and then set the property, or does it assume the property to be initialized already? Is this the

C# Object Initialiser - Reference to the new instance

做~自己de王妃 提交于 2019-11-27 08:20:37
问题 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 instance I'm creating. But it obviously refers the the instance of the class in which this code resides. I'm not asking if this is a good way to do this. I'm aware that I can do this like this: var x = new TestClass {Id= x}; x.SomeProperty = SomeMethod(this); I have a complicated scenario, in which a

Initial capacity of collection types, e.g. Dictionary, List

蓝咒 提交于 2019-11-27 04:11:39
Certain collection types in .Net have an optional "Initial Capacity" constructor parameter. For example: Dictionary<string, string> something = new Dictionary<string,string>(20); List<string> anything = new List<string>(50); I can't seem to find what the default initial capacity is for these objects on MSDN. If I know I will only be storing 12 or so items in a dictionary, doesn't it make sense to set the initial capacity to something like 20? My reasoning is, assuming that the capacity grows like it does for a StringBuilder, which doubles each time the capacity is hit, and each reallocation is

Order of operations using Object Initializer Syntax

[亡魂溺海] 提交于 2019-11-26 23:03:01
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? 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 sequence of member initializers, enclosed by { and } tokens and separated by commas. (Note the word "sequence" here,