views:

126

answers:

4

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 is 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.

A: 

Only if ISomeInterface is a class.

Update (for clarification):

Jon Skeet has a talk where he mentions default implementations for interfaces. They are not part of the C# language, though. The talk is about what Jon Skeet would like to see in a future version of C#.

For now, the only default implementations are done via (possibly abstract) base classes.

Stephen Cleary
Someone went through and dv'ed all the rational answers...
Stephen Cleary
I downvoted all the incorrect answers. because they were incorrect. no rationality about it. See Darins answer
Simon
I saw the "correct" answer. 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.
Stephen Cleary
@Setphen Agreed. edited the question so hopefully no one thinks it is a good idea.
Simon
Yet another unexplained downvote. Just love those...
Stephen Cleary
+2  A: 

Maybe you refer to Dependency Injection? Where when using DI framework (such as Ninject or Unity), you can define default instance for each interface and then using it like this:

(assuming you have IWeapon interface and Sword implements it)

IKernel kernel = new StandardKernel();
kernel.Bind<IWeapon>().To<Sword>();
var weapon = kernel.Get<IWeapon>();

But Ninject (and most other IoC frameworks) can do some more clever things, like: let's say we have the class Warrior that takes IWeapon as a parameter in its constructor. We can get an instance of Warrior from Ninject:

var warrior = kernel.Get<Warrior>();

Ninject will pass the IWeapon implementation we specified to the Warrior constructor method and return the new instance.

Other than that, I don't know of any in-language feature that allows this kind of behavior.

arikfr
good suggestion but not what i was looking for.See Darins Answer
Simon
Yes, I missed the part where you mentioned "black magic" in your question :-)Thanks for asking this -- I learned something new!
arikfr
A: 

As said Stephen the only way to give a default implementation by default in .NET is to use abstract classes.

But there is another way to constraints the implentation of your classes through an interface: Code Contracts. This technology from Microsoft Research has been integrated to the BCL of .NET 4.0. Using code contracts you can define an interface which implementation are checked statically or at runtime from a set of post, pre and invariant conditions.
You can get more informations here, a sample code contracts interface implementation here and a introductive Code Contracts technologies webcast here.

Regards.

Ucodia
+8  A: 

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 :-)

Darin Dimitrov
well i did say it was black magic. Thanks Darin.
Simon
And with the keyword "CoClass" the google gods are with me again.http://ayende.com/Blog/archive/2009/08/15/instantiating-interfaces.aspxhttp://marcgravell.blogspot.com/2009/08/who-says-you-cant-instantiate-interface.html
Simon
@Simon, those are two blogs I would recommend you tuning your RSS reader to :-)
Darin Dimitrov
@Daren already have them. but all my combinations of "interface default implementation" came up with naught. Signal to noise ratio was not good :)
Simon
@daren kind of funny that the answers saying "it is not possible" are geting upvoted after you posted your answer.
Simon
@Simon, that's because nobody likes black magic :-)
Darin Dimitrov
i tried to add a "blackmagic" tag but none exists.
Simon