473,508 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructors in a Static Class

Just learning OOP here... Does it make sense to have a constructor in a
static class? Say I have a class with a constructor and then mark that class
as static; will it (the default constructor) get called when the application
starts? or are any/all constructors ignored in static classes?

Thanks.
Jul 28 '06 #1
3 1391
Jordan S. wrote:
Just learning OOP here... Does it make sense to have a constructor in a
static class? Say I have a class with a constructor and then mark that class
as static; will it (the default constructor) get called when the application
starts? or are any/all constructors ignored in static classes?
It will not compile, unless the constructor is also marked static.
And yes, it may make sense, when you need lazy initialization.
Jul 28 '06 #2
static constructors are automatically executed (generally) when the static
class is first used by the runtime, which could be a while into your app.
But yes, they are definitely useful. For generic static classes the ctor is
executed once per template permutation.

Marc

Jul 28 '06 #3
"Jordan S." wrote:
Just learning OOP here... Does it make sense to have a constructor in a
static class? Say I have a class with a constructor and then mark that class
as static; will it (the default constructor) get called when the application
starts? or are any/all constructors ignored in static classes?
Static constructor make sense if you have static variables that you need to
initialize. Static constructors are invoked the first time the class is
accessed.
Be sure to mark the constructor as static.

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Jul 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21370
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
5721
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
1434
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
9
2415
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
8
2332
by: Pent | last post by:
Hi All, Why is this code valid? How can the static ctor be used? It doesn't act as class ctor at all. struct A { static A() { } }
5
4558
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
12
1751
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
12
8961
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
12
3408
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
4
1609
by: Adam | last post by:
Okay, so I know this will come off as a stupid question...but I am going to ask it anyways... I know you are not allowed to have constructors defined in an interface, but why not? I really...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7328
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,...
1
7049
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...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
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...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
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 ...
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.