Web Log di Adrian Florea

"You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
posts - 440, comments - 2715, trackbacks - 3944

My Links

Archives

Post Categories

Image Galleries

.RO Blogs

.RO People

.RO Sites

Blogs

Furls

Links

vinCitori

static constructor of struct (Nicu G. Fruja)

Se ci fosse stato class al post di struct, questo snippet:

using System;

struct P
{
  static P()
  {
    Console.WriteLine("Initializing P");
  }
}

class Test
{
  static void Main()
  {
    P p = new P();
  }
}

avrebbe stampato a console "Initializing P". Così invece, non stampa nulla! Dobbiamo quindi fare attenzione alla parola class in questo frammento di C# Language Specification (17.11):

"The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

  • An instance of the class is created.
  • Any of the static members of the class are referenced."

"The creation of the struct instance p using the parameterless default instance constructor P() does not trigger the execution of the static constructor of P", aggiunge Nicu Fruja.

Print | posted on lunedì 9 agosto 2004 19:35 | Filed Under [ Carillon .NET ]

Powered by:
Powered By Subtext Powered By ASP.NET