473,884 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1714
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*******@yaho o.com> wrote in message
news:f5******** *************** ***@posting.goo gle.com...
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*******@yaho o.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.co m>
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*******@yaho o.com> wrote in message
news:f5******** *************** ***@posting.goo gle.com...
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.co m> wrote in message news:<MP******* *************** **@news.microso ft.com>...
Coder Coder <co*******@yaho o.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
4606
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
15
6603
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
1928
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; // ... public static MyObject Object400;
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
9
2673
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 operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
12
2595
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 advance, Joe
55
6285
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
1
3548
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 following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
9
5860
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
14
6043
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10769
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10426
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.