Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define valid C# interface that cannot be implemented? [closed]

Tags:

c#

interface

I'm thinking about this (meta) question for couple of days already:
Is it possible to define valid C# interface, that cannot be implemented in any way?

Possible variations of this question: Is it possible to define such interface in C# 2.0, 3.0, 4.0, 5.0? Is it possible to define such interface that would not even compile when implemented, or that would compile but throw runtime exception?

Edit: I know such interface will be useless just by definition, but is a nice answer for lectures or testing applicants for a programming job how good they know C#.

like image 543
Martin Suchan Avatar asked May 27 '13 19:05

Martin Suchan


3 Answers

Is it possible to define valid C# interface that cannot be implemented?

This trivia question is not a good fit for StackOverflow, but what the heck, it is easily answered. (Wrongly, as it turns out! Read on!)

class C
{
    private C() {}
}
interface IFoo<T> where T : C, new()
{
}

IFoo<T> cannot be implemented for any T because there is no type argument that can be substituted for T. C doesn't work because C does not have a public parameterless constructor, and there can be no derived class of C because the default constructor is private. (Well, there could be an accessible derived class of C inside C, but there isn't in this case.)


UPDATE: Commenter "mike z" correctly points out that

class X<T> : IFoo<T> where T : C, new() {}

implements the interface, though of course now there is no way to instantiate X<T>!

Even better, user "GranBurguesa" points out that a derived class of C is permitted to be declared, just so long as it never calls the private constructor; this is only possible if it crashes and dies on instantiation. (Well, to be picky, it would also be permitted for the recursive calls to be optimized down to an infinite loop instead of a crash.)

Both devious workarounds pose a philosophical question: if an interface is implemented by a class no one can instantiate, is it really implemented? Of course GranBurguesa demonstrates that IFoo<D> can be implemented and constructed, so my answer is actually wrong.


There are also cases, such as the one hinted at in SLaks' deleted answer, in which an abuse of the generic mechanism leads to an "infinitary" type. Such types are not legal in the CLR; the C# design team has considered adding similar language to the C# compiler spec but hasn't gotten around to it yet. Use of these types can crash the compiler or the runtime.

For an example of an infinitary type that crashes the compiler, see my article:

To Infinity But Not Beyond


Here's one. Cut n paste this code into Visual Studio and you'll see that this interface cannot be implemented:

interface ΙAmAPerfectlyOrdinaryInterface { }

class C : IAmAPerfectlyOrdinaryInterface { }

like image 74
Eric Lippert Avatar answered Sep 30 '22 05:09

Eric Lippert


As long as we're talking trivia, I think this is a valid implementation of Eric Lippert's attempt:

class Program
{
    static void Main(string[] args)
    {
        D test = new D();
    }
}

class C
{
    private C() { }
}

interface IFoo<T> where T : C, new() { }

class D : C
{
    public D()
        : this(5) { }

    public D(int x)
        : this() { }
}

class Dfoo : IFoo<D> { }

It compiles fine but crashes with a StackOverflowException when you instantiate D.

like image 27
GranBurguesa Avatar answered Sep 30 '22 03:09

GranBurguesa


If you're trying to factor out an old interface you could mark the interface with the ObsoleteAttribute attribute.

Edit: as @Magnus noted in the comments, if you set the Error attribute to true it's usage will cause an error.

like image 43
Alex Avatar answered Sep 30 '22 04:09

Alex