Can we define a static class

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

We can define a static member in a class.
But can we define a static class, so that all members are static?
 
What do you mean by a static class? Are you thinking Singleton pattern so
that there is only one instance of this class?
 
AFAIK, nay,
but you might be interested in this:

public abstract class MyClass
{
private MyClass() { }
public static void DoSomething() { ...}
}

HTH,
F.O.R.

PS: the private constuctor *is* important :)
 
Thank,
I do'nt know what is Singleton pattern.

I want a static class so that every members in it can be static as the
module in vb.net

Ajay Kalra said:
What do you mean by a static class? Are you thinking Singleton pattern so
that there is only one instance of this class?

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
We can define a static member in a class.
But can we define a static class, so that all members are static?
 
I dont know VB.Net. I think a sealed class with a private constructor will
be close to what you want.

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
Thank,
I do'nt know what is Singleton pattern.

I want a static class so that every members in it can be static as the
module in vb.net

Ajay Kalra said:
What do you mean by a static class? Are you thinking Singleton pattern so
that there is only one instance of this class?

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
We can define a static member in a class.
But can we define a static class, so that all members are static?
 
Thank!
Can you give me an example?


Ajay Kalra said:
I dont know VB.Net. I think a sealed class with a private constructor will
be close to what you want.

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
Thank,
I do'nt know what is Singleton pattern.

I want a static class so that every members in it can be static as the
module in vb.net

Ajay Kalra said:
What do you mean by a static class? Are you thinking Singleton pattern so
that there is only one instance of this class?

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


We can define a static member in a class.
But can we define a static class, so that all members are static?
 
public sealed class CMyClass
{
private CMyClass() {}
public static void SomeMethod();
.....

}

Now you can use the static method as CMyClass.SomeMethod(). It will be good
if you can familiarize yourself with Singleton pattern. You will get away
from using static methods (which IMO is not really OO in any sense).

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
Thank!
Can you give me an example?


Ajay Kalra said:
I dont know VB.Net. I think a sealed class with a private constructor will
be close to what you want.

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


ad said:
Thank,
I do'nt know what is Singleton pattern.

I want a static class so that every members in it can be static as the
module in vb.net

"Ajay Kalra" <[email protected]> ¼¶¼g©ó¶l¥ó·s»D
:[email protected]...
What do you mean by a static class? Are you thinking Singleton
pattern
so
that there is only one instance of this class?

--
Ajay Kalra [MVP - VC++]
(e-mail address removed)


We can define a static member in a class.
But can we define a static class, so that all members are static?
 
Using "abstract" in this situation won't work. The compiler will
complain that the class contains no abstract methods or properties.

You should, as Ajay pointed out, use "sealed" here, which indicates
that no other classes can inherit from this one.
 
Hi,

The static classes will came with C# 2.0.
I don't remember that their members will
be "static" by default. Only what i remember
these classes will allow only static members.

cheers!
Marcin
 
Hi,

No, you cannot declare a class as static explicitely, You can do so
implicitely by declaring all the members as static. You could also declare
it as sealed to avoid inheritance.

cheers,
 
However, this ability is coming in V2.0

static class Foo
{
public static void Bar()
{
}
}

This creates a sealed abstract class so you cannot create an instance

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

No, you cannot declare a class as static explicitely, You can do so
implicitely by declaring all the members as static. You could also declare
it as sealed to avoid inheritance.

cheers,
 
Bruce Wood said:
Using "abstract" in this situation won't work. The compiler will
complain that the class contains no abstract methods or properties.

No it won't. Try it! It compiles with no problems.
You should, as Ajay pointed out, use "sealed" here, which indicates
that no other classes can inherit from this one.

You don't actually need either of them - just having a single private
constructor is sufficient.
 
Richard Blewett said:
However, this ability is coming in V2.0

static class Foo
{
public static void Bar()
{
}
}

This creates a sealed abstract class so you cannot create an instance

Not only that, but the class doesn't have *any* constructors, not even
a private one - something which is impossible to achieve with C# v1.
 
ad said:
We can define a static member in a class.
But can we define a static class, so that all members are static?

No.

But you can use all the members as static ( callable at runtime without
instantiation of the class ) so there is no need to.


That is

myStaticClass {



static MyMethod ()

}

is callable as:

myStaticCLass.MyMethod()

So why bother having a 'static class' when you can define all or some of
the methods and properties as static.
 
Because you also have to create a private default ctor to make sure people don't instantiate an instance. As this was quite a common requirement (the factory pattern) the C# team decided to formalize it for version 2.0 with the introduction of static classes.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


So why bother having a 'static class' when you can define all or some of
the methods and properties as static.
 
Back
Top