Since static methods don't belong to a particular object, they're not part of the API of the classes implementing the interface; therefore, they have to be called by using the interface name preceding the method name.
Static methods - They are declared using the static keyword and will be loaded into the memory along with the interface. You can access static methods using the interface name. If your interface has a static method you need to call it using the name of the interface, just like static methods of a class.
Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how.
Java interface static method helps us in providing security by not allowing implementation classes to override them. We can't define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”.
Interfaces can't have static members and static methods can not be used as implementation of interface methods.
What you can do is use an explicit interface implementation:
public interface IMyInterface
{
void MyMethod();
}
public class MyClass : IMyInterface
{
static void MyMethod()
{
}
void IMyInterface.MyMethod()
{
MyClass.MyMethod();
}
}
Alternatively, you could simply use non-static methods, even if they do not access any instance specific members.
You can't define static members on an interface in C#. An interface is a contract for instances.
I would recommend creating the interface as you are currently, but without the static keyword. Then create a class StaticIInterface
that implements the interface and calls the static C++ methods. To do unit testing, create another class FakeIInterface
, that also implements the interface, but does what you need to handle your unit tests.
Once you have these 2 classes defined, you can create the one you need for your environment, and pass it to MyClass
's constructor.
You can define static methods in c# 8 but you must declare a default body for it.
public interface IMyInterface
{
static string GetHello() => "Default Hello from interface" ;
static void WriteWorld() => Console.WriteLine("Writing World from interface");
}
or if you don't want to have any default body simply throw an exception:
public interface IMyInterface
{
static string GetHello() => throw new NotImplementedException() ;
static void WriteWorld() => throw new NotImplementedException();
}
Static members are perfectly legal in the CLR, just not C#.
You could implement some glue in IL to link up the implementation details.
Not sure if the C# compiler would allow calling them though?
See: 8.9.4 Interface type definition ECMA-335.
Interface types are necessarily incomplete since they say nothing about the representation of the values of the interface type. For this reason, an interface type definition shall not provide field definitions for values of the interface type (i.e., instance fields), although it can declare static fields (see §8.4.3).
Similarly, an interface type definition shall not provide implementations for any methods on the values of its type. However, an interface type definition can—and usually does—define method contracts (method name and method signature) that shall be implemented by supporting types. An interface type definition can define and implement static methods (see §8.4.3) since static methods are associated with the interface type itself rather than with any value of the type.
Beginning with C# 8.0, an interface may define a default implementation for members. It may also define static members in order to provide a single implementation for common functionality.
interface (C# Reference)
E.g.
public interface IGetSomething
{
public static string Something = "something";
}
var something = IGetSomething.Something;
They are considering adding some of those features in a future version of C# called C# "Ten". These theoretical features might allow static members on interfaces, alongside with roles. It would be huge step forward, it would allow generic operator overloading too, without any use of reflection. Here is an example snippet how it is planned to work, using the classic monoid example, which is just jargon for saying "something that can be added". Taken directly from Mads Torgersen: C# into the Future:
interface IMonoid<T>
{
static T Zero { get; }
static T operator +(T t1, T t2);
}
public static T AddAll<T>(T[] ts) where T : IMonoid<T>
{
T result = T.Zero;
foreach (T t in ts) { result += t; }
return result;
}
role IntAddMonoid extends int : IMonoid<int>
{
public static int Zero => 0;
}
IntAddMonoid[] values = new int[] {1, 2, 4, 8, 16, 32};
int sixtyThree = AddAll<IntAddMonoid>(values); // == 63
Additional resources:
Jeremy Bytes: C# 8 interface static members
EDIT
This post originally stated interface static members will be added in C# 8.0, which is not true, I misinterpreted Mads Torgersen's words in the video. The official C# 8.0 guide does not talk about static interface members yet, but it is clear they have been working on it for quite long now.
You could invoke it with reflection:
MyInterface.GetType().InvokeMember("StaticMethod", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With