public static class

T

Tony Johansson

Hello!

I have a class where all the members are static so I change the class to be
static but I have
run into one small problem and that is how do I code to prevent calling the
C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot have
instance C-tor.

//Tony
 
J

Jon Skeet [C# MVP]

I have a class where all the members are static so I change the class to be
static but I have run into one small problem and that is how do I code to
prevent calling the C-tor because
there is no point in creating an instance because all the members are
static.

For example I can't code private class XX because the class cannnot have
instance C-tor.

Static classes don't have constructors at all. The compiler doesn't
emit a default parameterless constructor for you. That's one of the
benefits of static classes.

Jon
 
J

jacky kwok

Peter said:
Right. But you also should not be able to instantiate a static class.

Do you have a code sample that violates that rule? That is, it allows
you to "new" an instance of a static class? The compiler should be
preventing it from happening for you. If it doesn't, that would be a bug.

Pete


Can you add the "Abstract" to the class?

I think you can add "static", that means there will be no any derived
class from this class.
Add "Abstract" to prevent any instance of the class and you can still
call any static function of the class.
 

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

Top