473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

C# Static

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
5 1673
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.