static constructor

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I have i utility class contains static functions (and also members) I
usually use in may applications.
Can i have a static like constructor so any time the app starts, The
constructor initializes static class members ?

Thanks,
Ali
 
A.M said:
I have i utility class contains static functions (and also members) I
usually use in may applications.
Can i have a static like constructor so any time the app starts, The
constructor initializes static class members ?

The static constructor won't run when the app starts - it'll run when
the first static member is accessed or the first instance is created.
 
But it doesn't allow me to create a static constructor it says:

c:\inetpub\wwwroot\AC\Utility.cs(13): 'AC.Utility.Utility()': access
modifiers are not allowed on static constructors

Here is my part of source :

namespace AC
{
public class Utility
{
public static Utility()
{
}
}
 
That merely says you can mark the constructor as being public or anything
else. That's because no one can ever call it explicitly, it will get called
by the .NET runtime only.

A.M said:
But it doesn't allow me to create a static constructor it says:

c:\inetpub\wwwroot\AC\Utility.cs(13): 'AC.Utility.Utility()': access
modifiers are not allowed on static constructors

Here is my part of source :

namespace AC
{
public class Utility
{
public static Utility()
{
}
}
 
A.M

You don't need an access modifier. The reason for this is that you can
not make the call to the static constructor explicitly (unlike an instance
constructor), so there is no need for an access modifier.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

A.M said:
But it doesn't allow me to create a static constructor it says:

c:\inetpub\wwwroot\AC\Utility.cs(13): 'AC.Utility.Utility()': access
modifiers are not allowed on static constructors

Here is my part of source :

namespace AC
{
public class Utility
{
public static Utility()
{
}
}
 
Hi Ali,

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.
From your discussion in this post, I think you need static constructor to
initialize the static members. Yes, just as Jon states, your class's static
constructor will not be called when application start(Unless you refer the
class member or instance when Application's initialize). A static
constructor is called automatically to initialize the class before the
first instance is created or any static members are referenced.

====================
Please refer to the MSDN static constructor document:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vclrfStaticConstructors.asp

In it Remarks part, you will find:
1). A static constructor does not take access modifiers or have parameters.
2). A static constructor cannot be called directly.
3).The user has no control on when the static constructor is executed in
the program.

So when you specify an access modifier, you will get " access modifiers are
not allowed on static constructors" error. This is almost the same as
normal instance constructor.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top