是否有可能定义不能实施有效的C#接口? [英] Is it possible to define valid C# interface that cannot be implemented?

查看:112
本文介绍了是否有可能定义不能实施有效的C#接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在考虑了几天,已经是这个(元)问题:结果
是否有可能定义有效的C#接口,不能以任何方式实施



这个问题的可能的变化:是否有可能在定义这样的接口 C#2.0,3.0,4.0,5.0 ?是否有可能定义这样的接口来实现时甚至不会编译或会编译,但抛出运行时异常



修改:我知道这样的接口将是无用的只是定义,但它是一个很好的答案讲座或测试申请人的编程工作如何好,他们知道C#。


解决方案

是否有可能定义不能实施有效的C#接口?




这小问题是不是非常适合计算器,但究竟发生了什么,这是很容易回答。 (错误,事实证明!请继续阅读!)

 类C 
{
私人C( ){}
}
接口IFoo的< T>其中T:C,新的()
{
}



的IFoo< T> 不能为任何实施 T ,因为没有可以取代无类型参数ŧ C ,因为 C 不具有公共的无参数的构造函数,而且不能有任何派生类的不起作用 C ,因为默认的构造函数是私有的。 (嗯,有可能是一个可访问的派生类的 C C ,但没有在这种情况下。 )






更新:评论麦克Z正确地指出

 类X< T> :的IFoo< T>其中T:C,新的(){} 



实现了接口,但当然现在还没有方法来实例化 X< T> !



更​​妙的是,用户GranBurguesa指出,派生类C的允许声明,只是只要它永远不会调用私有构造函数;如果它崩溃和死亡的实例,这是唯一可能的。

(嗯,挑剔的话,它也将允许递归调用被优化到一个无限循环,而不是崩溃。)

这两个狡猾的变通方法构成哲学问题:如果一个接口是由一类无人能实例化实现的,是不是真的执行?当然GranBurguesa表明,的IFoo< D​​> 可实施和建设,所以我的答案其实是错误的。






有也情况下,如一个暗示在SLaks'删除应答,其中,所述通用的滥用机制导致一个infinitary型。这种类型不是在CLR法律; C#的设计团队已经考虑增加类似语言C#编译器规范,但一直没有得到解决,以它。 。这些类型的使用可能会崩溃的编译器或运行时



对于崩溃编译器的infinitary类型的示​​例,请参阅我的文章:



到无穷大,但不超过






这里有一个。切n这个代码粘贴到Visual Studio中,你会看到这个界面无法实现的:



接口ΙAmAPerfectlyOrdinaryInterface{}



C类:IAmAPerfectlyOrdinaryInterface {}


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

解决方案

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 { }

这篇关于是否有可能定义不能实施有效的C#接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆