Wikipedia:Reference desk/Archives/Computing/2018 May 22

From Wikipedia, the free encyclopedia
Computing desk
< May 21 << Apr | May | Jun >> Current desk >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


May 22[edit]

csharp static property initialization[edit]

can you please clarify if, in the following code, will the constructor be called once only or every time the property is being accessed?

private static MyType Prop { get; set; } = new MyType()

cheers --Sigislao (talk) 17:01, 22 May 2018 (UTC)[reply]

The field will be initialized at most once before the first access. Per the C# language specification, "the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class." This also implies that if none of the static members of a class are ever referenced, the initialization need not occur at all. See [1]. -- Tom N talk/contrib 05:00, 23 May 2018 (UTC)[reply]
thanks for answering, anyway I suspect that fields and properties behave differently and that is a property actually — Preceding unsigned comment added by Sigislao (talkcontribs) 06:56, 23 May 2018 (UTC)[reply]