C #: "Unable to instantiate static class"

I am in the process of converting some Java code to C # and stumbled upon the following curious thing:

public interface IActivation { public abstract double func(double inputput); public static class S1 : IActivation { public double func(double input) { if (input > 0) return 1.0; return 0.0; } } } 

SomewhereElse (use):

 protected IActivation activation = new IActivation.S1(); 

Looking at the source code, he clearly understands what the goal is:

  • Declare an interface and embed several static implementations of this interface into it (the code contains other implementations of IActivation, for example, "S2", "S3", etc., which were omitted here).
  • A typical use case for this was to assign a variable to one specific implementation of this interface. In addition, by the way, you will need to create an instance of this variable, it is completely clear where these specific implementations belong - in the manner of saying, the enclosed declaration will further increase the readability of the code (for example, new IActivation.S1(); makes it clear that S1 is specific implementation of IActivation).

Interestingly, C # does not like the way to define the whole object: β€œ It is not possible to instantiate the static classβ€œ IActivation.S1 . ”Does anyone know how to reorganize this code so that 1 and 2 are saved.

+4
source share
5 answers

If IActivation does not have to be an interface, you can turn it into an abstract class

 public abstract class IActivation { public abstract double func(double inputput); public class S1 : IActivation { public override double func(double input) { if (input > 0) return 1.0; return 0.0; } } } 

This changes the actual meaning of the code, but allows you to say

 var s1 = new IActivation.S1(); 

Update The main problem that I can think of is if you have a class that extends something else and implements this interface, it will not work (you cannot inherit from two classes). Then you can create an interface and an abstract class that implements the abstract class, but that gets a little silly.

Another variant:

 public interface IActivation { // ... } public class Activation { public class S1 : IActivation { // ... } } 

The advantage is that you keep IActivation as an interface, but you have another class clogging your namespace.

In both cases, you did not make a direct port with Java.

+2
source

In Java, the inner static class has no hidden access to members of its closing type. In C #, all nested types do not have this access to their parent types; there is no modifier that needs to be added in C # to cause this behavior.

In C # classes, static abstract sealed , so they cannot be created or produced, is not the same value as in Java. In addition, interfaces cannot contain their own type declarations.

Try something like this:

 public interface IActivation { double Func(double inputput); } public class S1 : IActivation { public static readonly S1 Instance = new S1(); private S1() { } public double Func(double input) { if (input > 0) return 1.0; return 0.0; } } 

If your goal is to provide a default implementation in some β€œreadable” way (although I argue that IActivator.S1() is inherently more readable ...), you can create a static factory class:

 public static class Activator { public static S1 S1 { get { return S1.Instance; // Or you could do this if you make the S1 constructor public: // return new S1(); } } } 

However, I dispute the claim that it is more readable or useful. When creating an object in the context of a certain type, Visual Studio displays all subtypes of this type. So, if you do this ( | represents the cursor):

 IActivator foo = new | 

You should get a neat list of all the classes in your current area that implement IActivotor.

+11
source

Do not mark your class as static .

+6
source

The error message itself is understandable, the class S1 cannot be static, since you are creating an instance of it. Remove the static keyword from S1. In addition, the access modifier and abstract modifier are not valid in the interface declaration.

In C #, interfaces cannot declare internal types.

My suggestion here is to use the Factory pattern to get the right instances instead of nesting types in your interface (this increases the connection / dependencies).

 interface IActivation { double func(double inputput); } public static class ActivationFactory { IActivation GetImplA() { return new ImplA(); } IActivation GetImplB() { return new ImplB(); } } class ImplA : IActivation { } class ImplB : IActivation { } 
+2
source

use the sigletone pattern for each interface of the implementation of the S'i implementation and the gap and implementation, as described above, cdhowie

It doesn't seem like you need a factory - if your S'i instances don't have their own state?

0
source

All Articles