tags:

views:

2546

answers:

12

I am trying to inherit a non-static class by a static class.

public class foo
{ }

public static class bar : foo
{ }

And I get:

Static class cannot derive from type. Static classes must derive from object.

How can I derive it from object?

The code is in C#.

A: 

Doesn't all classes (static included) already derive from object? As in, by default?

Also, like it says, "Static class cannot derive from type.", so I don't think what you are doing is possible. Why would you want a static class to derive from a type anyways?

Svish
A: 

It can't. You have to create a normal class to derive from some other class.

Grzenio
+1  A: 

I don't think C# supports inheritance for static classes.

One option would be to use the singleton pattern instead

public class foo
{ }

public class bar : foo
{
    private bar instance;
    public bar GetInstance()
    {
        if(instance == null) instance = new bar();
        return instance;
    }

    private bar(){} //make default constructor private to prevent instantiation 
}
RSlaughter
Matt Olenik
I think your example is flawed, if two threads succeed passing the if(instance == null) it will instantiate a new bar two times.
Tomh
The above code generates error : Static classes cannot have instance constructors
Ali
Edited to remove static declaration on bar class.I didn't say it was thread safe, presumably you could put a lock on checking / creation of the instance.Personally I think singletons are going the way of goto, people assume they're always evil, which isn't always the case. DI can be overkill too
RSlaughter
A: 

I dont think that is possible. Why that is needed?

Sessiz Saat
+2  A: 

The error message is bogus. It's not saying "an" object. It's talking about the built-in type called "object" which is the base of everything in .NET.

It should say "static classes can not specify a base type".

GeekyMonkey
No, it should say "Static classes cannot specify a base type. Static classes always implicitly derive from System.Object." Note that you can't derive one static class from another static class either.
Jon Skeet
Corrected. Thanks Jon.
GeekyMonkey
A: 

All classes derive implicitly from Object. That said, although static classes (which by definition are just containers for static members) "derive" from object, there is nothing you can get from that fact.

Anton Gogolev
A: 

The error message is misleading.

bar can't inherit from foo because foo can be instantiated and bar can't.

teedyay
That's not a reason in itself. The C# spec could easily have allowed this case - it just happens not to.
Jon Skeet
Holy crap it's Jon Skeet!! *feels blessed or something* ;-)
teedyay
+13  A: 

From the C# 3.0 specification, section 10.1.1.3:

A static class may not include a class-base specification (§10.1.4) and cannot explicitly specify a base class or a list of implemented interfaces. A static class implicitly inherits from type object.

In other words, you can't do this.

Jon Skeet
+3  A: 

Taken from http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

The main features of a static class are:

They only contain static members.

They cannot be instantiated.

They are sealed.

They cannot contain Instance Constructors (C# Programming Guide).

So, inheriting from a non-static class violates the first feature of static classes on this list by introducing non-static members to your static class.

C.McAtackney
+1  A: 

Like stated earlier the C# spec say this can't be done. You can't implement a interface with static classes either. Your best bet is to change from using a static class to using a class that uses the singleton pattern. You will have only one instance (similar to how the static class works) and you will be able to inherit behavior or implement interfaces.

You read up on Singletons here, here, and here.

RS Conley
A: 

If you're trying to stop people creating instances of the class, just add a private default constructor.

Coder 42
My aim is to stop making instances of bar but the problem with private constructor would be that I would need to change variables and function in foo also to static to be able to access them.
Ali
In that case you should be using composition instead of inheritance; bar contains a static readonly instance of foo.
Coder 42
+4  A: 

There's no value in deriving static classes. The reasons to use inheritance are:

  • Polymorphism
  • Code reuse

You can't get polymorphism with static classes, obviously, because there is no instances to dynamically dispatch on (in other words, it's not like you can pass a Bar to a function expecting a Foo, since you don't have a Bar).

Code reuse is easily solved using composition: give Bar a static instance of Foo.

munificent