object-initializers

Accessing properties from object initializer [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-17 20:52:01
问题 This question already has answers here : C# Object Initialiser - Reference to the new instance (4 answers) Closed last year . I have the following Person class class Person { public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get { return FirstName + " " + LastName; } } public IEnumerable<Person> Children { get; set; } } I could initialize it like this: Person p = new Person() { FirstName = "John", LastName = "Doe" }; But is it possible to

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

拟墨画扇 提交于 2019-12-17 07:23:57
问题 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

using object initializer generates CA 2000 warning

不羁的心 提交于 2019-12-11 06:59:08
问题 Following code generates a CA2000 warning: Myclass myclass = null; try { myclass = new Myclass { Name = "a name" }; } finally { if (myclass != null) { myclass.Dispose(); } } i found some topics with the same problem and as I understand the problem is, that the compiler generates for the constructor a temporary variable and for this variable I'm not calling Dispose(). var tmp = new MyClass(); tmp.Name = "a name"; myclass = tmp: so my question is, if there is a solution with using object

Elasticsearch.NET NEST Object Initializer syntax for a highlight request

心不动则不痛 提交于 2019-12-11 03:07:55
问题 I've got: var result = _client.Search<ElasticFilm>(new SearchRequest("blaindex", "blatype") { From = 0, Size = 100, Query = titleQuery || pdfQuery, Source = new SourceFilter { Include = new [] { Property.Path<ElasticFilm>(p => p.Url), Property.Path<ElasticFilm>(p => p.Title), Property.Path<ElasticFilm>(p => p.Language), Property.Path<ElasticFilm>(p => p.Details), Property.Path<ElasticFilm>(p => p.Id) } }, Timeout = "20000" }); And I'm trying to add a highlighter filter but I'm not that

What is the behavior when there are more initializers than array size?

…衆ロ難τιáo~ 提交于 2019-12-10 23:29:45
问题 I would like to know what happens when there are more initializers than array size, e.g. : int t[3] = { 1, 2, 3, 4 }; Of course, my compiler warns it. I expected undefined behavior, but I didn't find any clause about it in C11 standard. So, did I miss something ? 回答1: The code is ill-formed in both C and C++. C++11 §8.5.1[dcl.init.aggr]/6 states: An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize. C11 §6.7.9/2 states:

Are class initializers possible in C#?

情到浓时终转凉″ 提交于 2019-12-10 20:23:14
问题 In C# you have object initializers to initialize fields of objects at creation time without using a constructor. Now I'm wondering if there is an equivalent to classes which means that you can 'initialize' properties of classes when defining subclasses without actually using an override syntax but simply declaring what the value of a known property is. Example: public abstract class Car { public abstract string Name { get; } } // usual approach public class Mustang : Car { public overwrite

How to debug object initializer code?

佐手、 提交于 2019-12-09 07:26:55
问题 Is there a way to step by step debug the object initializer code in Visual Studio? Example: return new Veranstaltung() { ID = tblVeranstaltung.VeranstaltungsID, Titel = tblVeranstaltung.Titel, KursNummer = tblVeranstaltung.Kursnummer, ErsterTermin = tblVeranstaltung.ersterTermin, Dauer = tblVeranstaltung.schulungsTage, StartZeit = tblVeranstaltung.BeginnZeit, EndZeit = tblVeranstaltung.Endzeit, KostenNettoValue = tblVeranstaltung.PreisNetto ?? default(decimal), IsLastMinute = tblVeranstaltung

Optimizing multiprocessing.Pool with expensive initialization

泄露秘密 提交于 2019-12-06 04:26:51
问题 Here is a complete simple working example import multiprocessing as mp import time import random class Foo: def __init__(self): # some expensive set up function in the real code self.x = 2 print('initializing') def run(self, y): time.sleep(random.random() / 10.) return self.x + y def f(y): foo = Foo() return foo.run(y) def main(): pool = mp.Pool(4) for result in pool.map(f, range(10)): print(result) pool.close() pool.join() if __name__ == '__main__': main() How can I modify it so Foo is only

covariant object initializers?

走远了吗. 提交于 2019-12-05 06:25:17
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 able to use that kind of initializer everywhere... The question is somewhat confusing, as the question

CodeDom and collection initializers

六月ゝ 毕业季﹏ 提交于 2019-12-05 02:27:58
问题 Is there a way to generate a dictionary initializer using the C# CodeDom? Are those supported at all? I would like to have: private IDictionary<string, string> map = new Dictionary<string, string> { { "Name", "Value" }, ... }; 回答1: This is not possible using the CodeDom constructs. They were not updated for collection initializers. LukeH has an excellent blog post on the subject of 3.5 features and the CodeDom http://blogs.msdn.com/b/lukeh/archive/2007/07/11/c-3-0-and-codedom.aspx 回答2: You