[Resolved] [1.0/1.1] Static initialization
Is there any static block for C#, can't seem to find a bit.
Code:
private static IList categories = new ArrayList();
static
{
categories.Add("News and Features");
categories.Add("Kababayan Edition");
categories.Add("Hometown");
categories.Add("Owning a Home");
}
?
Re: [1.0/1.1] Static initialization
Where does member initialisation get performed? In a constructor. Where does static member initialisation get performed? You guessed it. You should read this MSDN topic first.
Re: [1.0/1.1] Static initialization
Kewl. Thanks a bunch. I was just referring to java syntax and never tried to search. My bad. Again, thanks!
Re: [1.0/1.1] Static initialization
Quote:
Originally Posted by jmcilhinney
Where does member initialisation get performed? In a constructor. Where does static member initialisation get performed? You guessed it. You should read
this MSDN topic first.
Actually this code isn't "Member Initialization". in Java Initializers run before constructors, only once.
They are used for static and non-static members
Re: [Resolved] [1.0/1.1] Static initialization
My language was somewhat inaccurate. I should just said "initialisation" or "object initialisation". The first line of nebulom's code is initialisation of a member and it is executed before constructor code in C# also.
Re: [Resolved] [1.0/1.1] Static initialization
I'm not sure but I guess the first line doesn't have anything to do with the question.
I believe he was asking about
Code:
static
{
categories.Add("News and Features");
categories.Add("Kababayan Edition");
categories.Add("Hometown");
categories.Add("Owning a Home");
}
Re: [Resolved] [1.0/1.1] Static initialization
I realise that. I'm saying that the first line was initialising the member, i.e. assigning an initial value to the member variable, while the rest of the code would more correctly be termed "object initialisation", i.e. it is setting the new object being created to its initial state. I should have said in post #2 that "object initialisation" is done in a constructor. Initialisation of members can occur in a constructor or on the same line as their declarations, in which case it is executed before any constructors.