|
-
Jul 17th, 2006, 10:16 PM
#1
Thread Starter
Fanatic Member
[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");
}
?
Last edited by nebulom; Jul 18th, 2006 at 02:26 AM.
-
Jul 17th, 2006, 10:51 PM
#2
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.
-
Jul 17th, 2006, 11:36 PM
#3
Thread Starter
Fanatic Member
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!
-
Jul 18th, 2006, 02:14 AM
#4
Re: [1.0/1.1] Static initialization
 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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 18th, 2006, 02:43 AM
#5
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.
-
Jul 18th, 2006, 02:49 AM
#6
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");
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 18th, 2006, 03:17 AM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|