473,811 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Class and static methods

HL
Hi,

What is the difference between declaring a class as static and delaring
methods as static (class not static)?

Also, when do we use a static constructor?
Nov 23 '05 #1
9 1565
HL <HL@discussions .microsoft.com> wrote:
What is the difference between declaring a class as static and delaring
methods as static (class not static)?
Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.
Also, when do we use a static constructor?


When you need to perform some initialisation for the type itself -
typically setting up things like maps which are used elsewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.


Does that work in C#? Help says static doesn't apply to types.

Michael
Nov 23 '05 #3
"Michael C" <no****@nospam. com> wrote in message
news:es******** ******@TK2MSFTN GP12.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.


Does that work in C#? Help says static doesn't apply to types.

Michael


Its a feature new to version 2.0 of C#

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 23 '05 #4
HL
Hi,

Your explanation on Static class was helpful. But I didn't get the
static constructor part. Let's say I have a class that has a static
constructor, and I attempt to create 2 instances of that type , will there
be 1 instance or 2 instances? I think I am confusing between Singleton and
static here. Can you please give a detailed explanation.
"Jon Skeet [C# MVP]" wrote:
HL <HL@discussions .microsoft.com> wrote:
What is the difference between declaring a class as static and delaring
methods as static (class not static)?


Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.
Also, when do we use a static constructor?


When you need to perform some initialisation for the type itself -
typically setting up things like maps which are used elsewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 23 '05 #5
Yes it works in C#.
You can declare a class static, and you can also declare a static
constructor.
I think the static class feature was only added in framework v2.
Sharon.

"Michael C" <no****@nospam. com> wrote in message
news:es******** ******@TK2MSFTN GP12.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.


Does that work in C#? Help says static doesn't apply to types.

Michael

Nov 23 '05 #6
Hi.
1. You can not instanciate a static class.
2. The static constructor is invoked, when the class is loaded.
Saron.

"HL" <HL@discussions .microsoft.com> wrote in message
news:34******** *************** ***********@mic rosoft.com...
Hi,

Your explanation on Static class was helpful. But I didn't get the
static constructor part. Let's say I have a class that has a static
constructor, and I attempt to create 2 instances of that type , will
there
be 1 instance or 2 instances? I think I am confusing between Singleton and
static here. Can you please give a detailed explanation.
"Jon Skeet [C# MVP]" wrote:
HL <HL@discussions .microsoft.com> wrote:
> What is the difference between declaring a class as static and
> delaring
> methods as static (class not static)?


Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.
> Also, when do we use a static constructor?


When you need to perform some initialisation for the type itself -
typically setting up things like maps which are used elsewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 23 '05 #7
"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:%
Its a feature new to version 2.0 of C#


That would explain why it's not working here :-)

Michael
Nov 23 '05 #8
HL
Some useful information on Static constructors - here:
http://www.ondotnet.com/pub/a/dotnet...or.html?page=1
"Sharon" wrote:
Hi.
1. You can not instanciate a static class.
2. The static constructor is invoked, when the class is loaded.
Saron.

"HL" <HL@discussions .microsoft.com> wrote in message
news:34******** *************** ***********@mic rosoft.com...
Hi,

Your explanation on Static class was helpful. But I didn't get the
static constructor part. Let's say I have a class that has a static
constructor, and I attempt to create 2 instances of that type , will
there
be 1 instance or 2 instances? I think I am confusing between Singleton and
static here. Can you please give a detailed explanation.
"Jon Skeet [C# MVP]" wrote:
HL <HL@discussions .microsoft.com> wrote:
> What is the difference between declaring a class as static and
> delaring
> methods as static (class not static)?

Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.

> Also, when do we use a static constructor?

When you need to perform some initialisation for the type itself -
typically setting up things like maps which are used elsewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Nov 23 '05 #9
Michael C <no****@nospam. com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Declaring the class static forces everything in it to be static and
makes sure there are *no* constructors - not even a private one, or a
parameterless one supplied by the compiler.


Does that work in C#? Help says static doesn't apply to types.


It's a new feature of C# 2.0.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #10

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

Similar topics

4
8037
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned in "Learning Python" by Mark Lutz and David Ascher. It seems like they are a relatively new feature... It seems to me that any truly OO programming language should support these so I'm sure that Python is no exception, but how can these be...
3
1999
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one from Guido: "The new descriptor API makes it possible to add static methods and class methods. Static methods are easy to describe: they behave pretty much like static methods in C++ or Java." http://www.python.org/2.2.3/descrintro.html
8
52760
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind it. thanks, Steven
3
9762
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx mauzi
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...
11
3851
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
6
1778
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic .... CallToStaticMethod();
2
5487
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
4
2995
by: DBC User | last post by:
I have a class with bunch of static methods. I could regourp all the static methods into seperate 3 or 4 classes. I was thinking about using Singlton pattern for all these 4 classes so that it behaves like static still class. But my concern is, when going from static to singlton, I need to add one more line to make sure the class is instantiated. Am I going in right direction? Thanks for the help.
9
5852
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);
0
10648
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
10389
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...
0
10135
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
9205
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...
1
7670
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.