51

I want to take a class I have and split it up into several little classes so it becomes easier to maintain and read. But this class that I try to split using partial is a static class.

I saw in an example on Stackoverflow that this was possible to do but when I do it, it keeps telling me that I cannot derive from a static class as static classes must derive from object.

So I have this setup:

public static class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class MachineFacade : Facade
{
    // Methods that are specifically for Machine Queries in our Database
}

Any pointers? I want the Facade class to be static so that I don't have to initialize it before use.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • 8
    Jop thats right. It's not the problem with the partial class. The problem is that you trying to derive from a static class, which is not possible. – ckruczek Feb 22 '16 at 08:36
  • Why does the class need to be statric at all? – Tim Schmelter Feb 22 '16 at 08:40
  • @TimSchmelter Because it's a method in which you have a Facade to access network or databases. In this case the upper layers don't really care about the implementation of data retrieval. It simply calls the right method in the Facade so you can keep changing the Facade if code needs to change or be extended. I don't really want to initialize this class in every class that needs to touch it. – OmniOwl Feb 22 '16 at 08:42
  • 1
    @Vipa: you could use singleton pattern to use one instance. Then you could remove the `static` from the classes and inherit `MachineFacade` from `Facade`. The methods can also be `static`. – Tim Schmelter Feb 22 '16 at 08:45
  • @TimSchmelter What benefits would I get compared to my current setup? Would I run into problems with the singleton pattern if I wanted to use the Facade with multi threading? – OmniOwl Feb 22 '16 at 08:54
  • @Vipar: reusability, maintainability/readability(logical structure through inheritance), not sure about polymorphism. If you need thread-safety with singletons read [this](http://csharpindepth.com/Articles/General/Singleton.aspx) article from Jon Skeet. Other pros: you can implement interfaces, not all methods must be static, singleton can be lazily loaded, you can clone/copy a singleton but not a static class. – Tim Schmelter Feb 22 '16 at 09:06

6 Answers6

56

Keep naming and modifiers consistent across files:

public static partial class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class Facade
{
    // Methods that are specifically for Machine Queries in our Database
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • Hm okay. I looked at how Visual Studio did it with WPF where it has a setup like `public partial class MainWindow : Window` so I had this idea that it was required. Thanks! – OmniOwl Feb 22 '16 at 08:37
  • I put different classes in appropriately named files. For example: Facade.cs, Facade_MachineQueries.cs – Amadeusz Wieczorek Feb 22 '16 at 08:39
  • Yeah I just realized C# doesn't care. I'm so used to Java haha – OmniOwl Feb 22 '16 at 08:39
  • @Vipar it means "MainWindow is a partial class and MainWindow is inheriting from Window". Window could be a non partial class. You are welcome, glad to help ;) – Guillaume Feb 22 '16 at 08:42
  • Yeah I had this idea that it was required for the compiler to know what classes to put together but I see now it's purely for inheritance (the operator). – OmniOwl Feb 22 '16 at 08:44
  • 3
    Modifiers don't need to be the same, except for clarity for a human reader. If they're all specified in one file, the other can just have `partial class` and it will use the modifiers from the first file. – George T Feb 22 '16 at 11:46
  • 1
    It is worth mentioning that all extensions for a specific partial class must share the same namespace. – Lars Gyrup Brink Nielsen Jan 09 '18 at 07:40
12

The problem is not that the class is a partial class. The problem is that you try to derive a static class from another one. There is no point in deriving a static class because you could not make use Polymorphism and other reasons for inheritance.

If you want to define a partial class, create the class with the same name and access modifier.

Markus
  • 20,838
  • 4
  • 31
  • 55
  • It can make a lot of sense to derive one static class from another, especially in a language like C# that abounds with unnecessary arbitrary limitations that were deliberately tacked on (obviously predicated on the theory that all programmers must be drooling morons). One obvious use that springs to mind is namespaces housing functions related to protocol/format specifications, which in C# must necessarily be stuck in some class. So you have 'class' Specification_v101 that derives from Specification_v100, inheriting everything that's unchanged and able to call upon v100 for implementing things – DarthGizka Jun 04 '17 at 14:55
5

you do not need to override anything, just give them the same name:

public static partial class Facade
{
    // this is the 1st part/file
}

public static partial class Facade
{
    // this is the 2nd part/file
}
VDN
  • 717
  • 4
  • 12
  • Yes, this is good practice. However you don't even have to mark the partial classes with the static keyword but only the main (or first) one. – Mitulát báti Dec 12 '22 at 17:03
5

You can not inherit a static class.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

NoName
  • 7,940
  • 13
  • 56
  • 108
3

C# doesn't support inheritance from a static class.

You have to choose between your classes being static:

public static class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class MachineFacade
{
    // Methods that are specifically for Machine Queries in our Database
}

...or whether you wish MachineFacade to derive from Facade:

public class Facade
{
    // A few general methods that other partial facades will use
}

public partial class MachineFacade : Facade
{
    // Methods that are specifically for Machine Queries in our Database
}
WonderWorker
  • 8,539
  • 4
  • 63
  • 74
2

Although I am too late for the party...
According to your problem description, I think your goals are:

  1. Want a static method group.
  2. Want a sub-group of it to be specialized for certain domain.

In this case, I would suggest using nested classes:

public static class Facade
{
    // some methods

    public static class Machine
    {
        // some other methods
    }
}

Pros:

  1. Methods are static. You can use them directly without initialize any instances.
  2. The nested class names are used like namespaces. Readers of your code knows clearly that Facade.Machine methods are more specific than Facade ones and their target domain.
  3. You can make methods with the same signature in different class level doing different things to mimic method overriding.

Cons:

  1. There is no way to forbids users calling Facade.Machine methods when they should not.
  2. No inheritance. Facade.Machine does not automatically have methods from Facade.
YAC
  • 405
  • 4
  • 14