1

For few reasons, static classes cannot implement interfaces, and I wonder what is the best practice for implementing two static classes that has the same methods with different implementations.

First class:

public static class MyStaticClass1
{
    public static void DoSomething()
    {
        // Do Something here...
    }
}

Second class:

public static class MyStaticClass2
{
    public static void DoSomething()
    {
        // Do Something here...
    }
}

The methods must be static since they are using the [DllImport] attribute which can be used on static methods only.

Depending on some runtime condition, In some runs I'll be using MyStaticClass1, and MyStaticClass2 on others, but not both in the same run.

Ideas?

  • 1
    Does this answer your question? [What is a singleton in C#?](https://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c) and [Singleton Pattern for C#](https://stackoverflow.com/questions/2667024/singleton-pattern-for-c-sharp) and [Thread Safe C# Singleton Pattern](https://stackoverflow.com/questions/12316406/thread-safe-c-sharp-singleton-pattern) –  May 26 '21 at 20:15
  • Suggested duplicates are for the question title: "*Alternative for static classes implementing interface*"... but regarding the body, I don't see the relation with DllImport and no interface is mentioned. However, you can redirect any interface property and method implemention to static members, thus the singleton will be a wrapper to statics. –  May 26 '21 at 20:16
  • @OlivierRogier the DllImport is mentioned to avoid solutions that will omit the static methods, and interface is mentioned in the first line of the question. – FriendOfFriend May 26 '21 at 20:19
  • @.FriendOfFriend Is the suggested design suitable? –  May 26 '21 at 20:20
  • @OlivierRogier No since Static member cannot be accessed with an instance reference – FriendOfFriend May 26 '21 at 20:25
  • @.FriendOfFriend Of course yes, I do this every hour of code, sometimes to solve cases like this one as well as having many instances accessing common static members. [static (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static) –  May 26 '21 at 20:26
  • @OlivierRogier (https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0176?f1url=%3FappId%3Droslyn%26k%3Dk(CS0176)) – FriendOfFriend May 26 '21 at 20:29
  • 2
    @.FriendOfFriend Yes! To access a public static member `MyMember` of `MyClass` from the outside, you must use `MyClass.MyMember` and not `myInstance.MyMember`... And from the inside, you simply write `MyMember`. Thus you can provide instance members to implement interface that wraps static members. Do you see what I mean? –  May 26 '21 at 20:32
  • Yea, I see what you mean and that actually means to delegate a call to a matching static one. That means that every method will have two signatures, one which isn't static for outside calls, and an internal static one. I'd rather avoid the duplication. – FriendOfFriend May 26 '21 at 20:36
  • 2
    @.FriendOfFriend No other way: static classes cannot implement interfaces, and static methods of non-static classes cannot be interface implementations. I know it is annoying, therefore the only solution is that, as I know: an instance wrapper to implement interfaces, toward statics, and once the hard work will be done, you'll be ok and happy. I don't think writing an answer is necessary, and if you want only one instance, singleton is what you need. –  May 26 '21 at 20:38

0 Answers0