Amazon.com Widgets

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

 

Published 10 October 04 04:41 by BradA
Filed under:

Comments

# Roy Osherove said on October 10, 2004 5:12 PM:
does this count? (it compiles alright)
interface IFoo
{

}

public class Class1
{
static void Main()
{
new IFoo();
}

class IFoo
{

}

}
# Kris said on October 10, 2004 8:06 PM:
I came up with the same thing... an inner class within Class1 and it compiles fine.
# David James said on October 10, 2004 8:18 PM:
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.)
# Kevin Dente said on October 10, 2004 8:59 PM:
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...
# Judah Himango said on October 10, 2004 9:26 PM:
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! :-)
# Wilco Bauwer said on October 10, 2004 11:07 PM:
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.)
# Jeff Parker said on October 11, 2004 6:26 AM:
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
{}
# Jeff Parker said on October 11, 2004 7:10 AM:
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");
}
}
# Kevin Westhead said on October 11, 2004 10:11 AM:
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.
# Ilya Haykinson said on October 11, 2004 3:46 PM:
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.
# Alex Campbell said on October 12, 2004 2:14 AM:

Was the interop answer the one you had in mind, Brad?
# Oleg Stepanov's Weblog said on October 21, 2004 8:07 AM:
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...
New Comments to this post are disabled

Search

Go

This Blog

Syndication

Page view tracker