简体   繁体   中英

How can a static class derive from an object?

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#.

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 instance 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.

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.

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".

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.

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 
}

As Christopher have pointed out, suppose we can derive static class from non-static class.

for example:

public class foo
{
    public int myVar;
}

public static class bar : foo { }

here, bar class derived from foo , hence static class Bar has non-static member myVar now, and According to the c# Specification , Static Class can not contains non-static members!

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 .

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?

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

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.

The error message is misleading.

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

如果您试图阻止人们创建类的实例,只需添加一个私有默认构造函数。

static class can not be base class for other classes and it can not be extended.

static class can be inherited only from "object" class(.net base class).

Additional information: All classes in .net inherited from "object" class, even any static class also.

The difference is even though static class inherit "object class" it inherit only two static methods from "object" class.

Other non-static classes inherit other four methods.

The OP asked "How can I drive it from object?". The object here is not an instance of another class so the answer is you don't. The object refers to the System.Object base class which is the default base class for all types in .NET. The error message is bit misleading.

Just for completeness, on the flip side, a static class also cannot be inherited or extended because it is both an abstract and a sealed class.

As said in other answer, the compiler message is confusing but correct. You can write:

static class A : object
{
}

of course it is not really useful to explicitly state this inheritance but the inheritance itself is actually useful in the framework as you override the object's virtual members:

    static class A : object
    {
        public static new string ToString()
        {
            return "I am object A";
        }

        public static new int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM