Static Class and static methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
HL said:
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 said:
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
 
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.
 
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.
 
Hi.
1. You can not instanciate a static class.
2. The static constructor is invoked, when the class is loaded.
Saron.
 
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top