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

= May 22 =

csharp static property initialization
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)


 * 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 . --  Tom N  talk/contrib 05:00, 23 May 2018 (UTC)


 * thanks for answering, anyway I suspect that fields and properties behave differently and that is a property actually — Preceding unsigned comment added by Sigislao (talk • contribs) 06:56, 23 May 2018 (UTC)