472,364 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1452
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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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**************@TK2MSFTNGP12.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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**************@TK2MSFTNGP12.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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**********************************@microsof t.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.com>
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**********************************@microsof t.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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
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...
3
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...
8
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...
3
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 ...
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...
11
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...
6
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...
2
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
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...
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.