Quiz: Instantiating an interface


I got this one from a reader down under… I gotta say, it pretty much had me stumped, so I thought I’d share it with you…

 

Compile the following code and you get this error message:

Cannot create an instance of the abstract class or interface ‘IFoo’    

 

interface IFoo { }

public class Class1

{

    static void Main()

    {

        new IFoo();

    }

}

 

Of course you say, you can never create an instance of an interface, only an instance of a concrete class that implements an interface.  It says so right in the error message.  

Well, not so.  Your quiz today is to only add lines above and below the code snippet above to make it compile cleanly. (No comments or #define magic here please).

 

Comments (12)

  1. Roy Osherove says:

    does this count? (it compiles alright)

    interface IFoo

    {

    }

    public class Class1

    {

    static void Main()

    {

    new IFoo();

    }

    class IFoo

    {

    }

    }

  2. Kris says:

    I came up with the same thing… an inner class within Class1 and it compiles fine.

  3. David James says:

    In the original quiz, IFoo was already a private nested interface, so no, it doesn’t count 🙂

    (I sent the quiz around the office, and one of my colleagues forwarded it to Brad, but I guess it mutated along the way.)

  4. Kevin Dente says:

    Compile, you say? This will make it compile:

    [System.Runtime.InteropServices.ComImport()]

    [System.Runtime.InteropServices.Guid("D95FCCE0-F518-46c4-87CA-D9FCED7223A1")]

    [System.Runtime.InteropServices.CoClass(typeof(Class1))]

    interface IFoo { }

    But I’m not sure if that’s what you’re looking for…

  5. Judah Himango says:

    Stumped! Looked at this one for 10 minutes and best I could come up with was a nested class named IFoo, but David says this isn’t allowed. Looking forward to seeing the solution! 🙂

  6. Wilco Bauwer says:

    Kevin is right. This topic is most likely about "class interfaces". Whenever you import an object with the type library importer, those class interfaces (RCW) are generated (together with a class which’s name is suffixed with ‘Class’ (also RCW)).

    (There are exceptions though, when objects for example only implement interfaces like IUnknown or IDispatch. They are never exposed as RCW classes because the members are internally called by the .NET framework.)

  7. Jeff Parker says:

    You can’t instantiate an interface it must have a specific method, this is from my understanding, but you can new up the specific class that implements the interface and return it.

    I guess I would want to know more as to the reason to have a new interface that isn’t implemented and has no code in it but the following would be usefull as you can get specific implementation back of an interface.

    interface IFoo { }

    public class Class1

    {

    static void Main()

    {

    IFoo moreFoo = ReturnFoo(1);

    }

    static internal IFoo ReturnFoo(int fooClassToReturn)

    {

    if(fooClassToReturn = 1)

    {

    FooClass1 foo = new FooClass1();

    return foo;

    }

    else

    {

    FooClass2 foo = new FooClass2();

    return foo;

    }

    }

    }

    public class FooClass1: IFoo

    {}

    public class FooClass2: IFoo

    {}

  8. Jeff Parker says:

    Ooops small error with the if statement only one = sign, but this is more of an explanation, as you are gaurenteed to new up a class but the imlpementation is completely different.

    interface IFoo

    {

    void WriteIt();

    }

    public class Class1

    {

    static void Main()

    {

    IFoo moreFoo = ReturnFoo(1);

    IFoo moreFoo2 = ReturnFoo(2);

    moreFoo.WriteIt();

    moreFoo2.WriteIt();

    }

    static internal IFoo ReturnFoo(int fooClassToReturn)

    {

    if(fooClassToReturn == 1)

    {

    FooClass1 foo = new FooClass1();

    return foo;

    }

    else

    {

    FooClass2 foo = new FooClass2();

    return foo;

    }

    }

    }

    public class FooClass1: IFoo

    {

    public void WriteIt()

    {

    Console.WriteLine("This is Foo 1");

    }

    }

    public class FooClass2: IFoo

    {

    public void WriteIt()

    {

    Console.WriteLine("This is Foo 2");

    }

    }

  9. Kevin Westhead says:

    I agree with Kevin Dente’s comments. The use of the CoClass attribute allows you to write code that appears to instantiate an interface, although you are in fact instantiating an instance of the type identified in the CoClass attribute ctor.

  10. A conjecture — perhaps it’s possible to set up a transparent proxy for the interface and mark the interface with a proxy attribute? I have tried this and failed, but I don’t know everything there is to know about proxy creation.

  11. Was the interop answer the one you had in mind, Brad?

  12. Recently I have been writing a managed interface to metadata and found several interesting things which I didn’t know before. One of these things can be easily demonstrated with C# code, so I decided to make a quiz (just like…