By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

C# Static

P: n/a
Hello,
Is it possible in C# to have the following.

public class Foo
{
static
{
}

public Foo ()
{
}
}

Having a section of code which is static and is executed at the
startup of the program.

- Thanks
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Yes, it is, with a slight adjustment.

It should be
static Foo()
{
}

and it is a static constructor when the class is touched for the first time.

Miha

"Coder Coder" <co*******@yahoo.com> wrote in message
news:f5**************************@posting.google.c om...
Hello,
Is it possible in C# to have the following.

public class Foo
{
static
{
}

public Foo ()
{
}
}

Having a section of code which is static and is executed at the
startup of the program.

- Thanks

Nov 15 '05 #2

P: n/a
Coder Coder <co*******@yahoo.com> wrote:
Is it possible in C# to have the following.

public class Foo
{
static
{
}

public Foo ()
{
}
}

Having a section of code which is static and is executed at the
startup of the program.


It's not executed at the start of the program - it's executed when the
type is first initialized (ie immediately prior to its first use).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
Thus spake Coder Coder:
Hello,
Is it possible in C# to have the following.
Almost. Change static {} to static Foo() {} and you're in business.
Having a section of code which is static and is executed at the
startup of the program.


Subtle difference: static constructors are called prior to the
initialization of the first instance of that class or the first use of a
static method, not upon program startup.

Check the help on "static constructors" for detailed explanations.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
Nov 15 '05 #4

P: n/a
I'd just like to add to what's gone before that the static constructor
(which is what it's called) is run after the initialization of any static
variables that have explicit initializations.

Tom Dacon
Dacon Software Consulting

"Coder Coder" <co*******@yahoo.com> wrote in message
news:f5**************************@posting.google.c om...
Hello,
Is it possible in C# to have the following.

public class Foo
{
static
{
}

public Foo ()
{
}
}

Having a section of code which is static and is executed at the
startup of the program.

- Thanks

Nov 15 '05 #5

P: n/a
Yes, the syntax is:

public class Foo()
{
public static Foo()
{
}

public Foo()
{
}
}
Jon Skeet <sk***@pobox.com> wrote in message news:<MP************************@news.microsoft.co m>...
Coder Coder <co*******@yahoo.com> wrote:
Is it possible in C# to have the following.

public class Foo
{
static
{
}

public Foo ()
{
}
}

Having a section of code which is static and is executed at the
startup of the program.


It's not executed at the start of the program - it's executed when the
type is first initialized (ie immediately prior to its first use).

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.