Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the default implementation of an interface in c#?

There is some black magic code in c# where you can define the default implementation of an interface.

So you can write

var instance = new ISomeInterface(); 

Any pointers?

UPDATE 1: Note that I did not ask if this is a good idea. Just how was it possible to do it.

UPDATE 2: to anyone seeing the accepted answer.

  • "this should be treated merely as a curiosity." from Marc Gravel "Newing up" Interfaces
  • "It's a bad idea to use a tool designed for COM interop to do something completely and utterly different. That makes your code impossible to understand for the next guy who has to maintain it" from Eric Lippert "Newing up" Interfaces
  • "While it may work, if it were ever found in production code by a rational coder, it would be refactored to use a base class or dependency injection instead." from Stephen Cleary in a comment below.
like image 387
Simon Avatar asked Jul 17 '10 11:07

Simon


People also ask

Can we have default implementations in the interface?

Interfaces can have default methods with implementation in Java 8 on later. Interfaces can have static methods as well, similar to static methods in classes. Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.

What is default interface implementation C#?

Default interface methods enable an API author to add methods to an interface in future versions without breaking source or binary compatibility with existing implementations of that interface. The feature enables C# to interoperate with APIs targeting Android (Java) and iOS (Swift), which support similar features.

Can we have default implementation in the interface in C#?

With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface.

What is default method in interface?

You specify that a method definition in an interface is a default method with the default keyword at the beginning of the method signature. All method declarations in an interface, including default methods, are implicitly public , so you can omit the public modifier.


1 Answers

Here comes the black magic:

class Program {     static void Main()     {         IFoo foo = new IFoo("black magic");         foo.Bar();     } }  [ComImport] [Guid("C8AEBD72-8CAF-43B0-8507-FAB55C937E8A")] [CoClass(typeof(FooImpl))] public interface IFoo {     void Bar(); }  public class FooImpl : IFoo {     private readonly string _text;     public FooImpl(string text)     {         _text = text;     }      public void Bar()     {         Console.WriteLine(_text);     } } 

Notice that not only you can instantiate an interface but also pass arguments to its constructor :-)

like image 62
Darin Dimitrov Avatar answered Sep 22 '22 07:09

Darin Dimitrov