为什么在实现接口时C#不允许继承返回类型

时间:2022-09-02 11:57:43

Is there any rational reason why the code below is not legal in C#?

有没有合理的理由说明为什么下面的代码在C#中不合法?

class X: IA, IB
{
    public X test() // Compliation Error, saying that X is not IB
    {
        return this;
    }
}

interface IA 
{
    IB test();
}
interface IB { };

6 个解决方案

#1


29  

This feature is called "return type covariance". C# does not support it for the following reasons:

此功能称为“返回类型协方差”。由于以下原因,C#不支持它:

1) The CLR doesn't support it. To make it work in C#, we'd have to just spit a whole bunch of little helper methods that do casts on the return type to the right thing. There's nothing stopping you from doing that yourself.

1)CLR不支持它。为了使它在C#中工作,我们必须吐出一大堆小助手方法,这些方法将返回类型转换为正确的东西。没有什么能阻止你自己这样做。

2) Anders believes that return type covariance is not a good language feature.

2)Anders认为返回类型协方差不是一个好的语言特征。

3) \We have lots of higher priorities for the language. We have only limited budgets and so we try to only do the best features we can in any given release. Sure, this would be nice, but it's easy enough to do on your own if you want to. Better that we spend the time adding features that improve the developer experience or add more representational power to the language.

3)\我们有很多更高的语言优先级。我们的预算有限,因此我们只尝试在任何给定的版本中尽我们所能。当然,这会很好,但如果你愿意的话,这很容易就可以自己做。更好的是,我们花时间添加可改善开发人员体验的功能或为语言添加更具代表性的功能。

#2


15  

You could use explicit interface implementation to avoid the problem.

您可以使用显式接口实现来避免此问题。

class  X : IA, IB
{
  public X test()
  {
    return this;
  }

  IB IA.test()
  {
    return this;
  }
}

interface IA
{
  IB test();
}

interface IB
{
}

#3


5  

The signatures must match exactly to what the interface specifies. There's no reason you cannot return an instance of X from the method, but the method signature will have to use IB.

签名必须与接口指定的完全匹配。没有理由不能从方法返回X的实例,但方法签名必须使用IB。

As for a rational reason.. it's probably preferable from a code readability point of view.

至于理性的原因......从代码可读性的角度来看,这可能是最好的。

You could implement the interface explicitly, and provide an alternative signature that returns X that is not defined by the interface. If you know your IA is actually an X, you could use that instead.

您可以显式实现接口,并提供一个替代签名,该签名返回未由接口定义的X.如果您知道您的IA实际上是X,那么您可以使用它。

#4


3  

Because C# does not support co and contravriance for interfaces in compile time. This way an implementation of IA.Test() method must exactly match its declaration. You can, however, return instance of X in runtime

因为C#在编译时不支持接口的co和contravriance。这样,IA.Test()方法的实现必须与其声明完全匹配。但是,您可以在运行时返回X的实例

#5


1  

public X test();

You must declare a body for all methods in any class that's not abstract.

您必须为任何不抽象的类声明所有方法的主体。

Try this:

尝试这个:

class X : IA, IB
{
    public IB test()
    {
        return new X();
    }
}

interface IA
{
    IB test();
}
interface IB { };

#6


1  

This can help http://geekswithblogs.net/abhijeetp/archive/2010/01/10/covariance-and-contravariance-in-c-4.0.aspx You can use the "out" keyword

这可以帮助http://geekswithblogs.net/abhijeetp/archive/2010/01/10/covariance-and-contravariance-in-c-4.0.aspx您可以使用“out”关键字

#1


29  

This feature is called "return type covariance". C# does not support it for the following reasons:

此功能称为“返回类型协方差”。由于以下原因,C#不支持它:

1) The CLR doesn't support it. To make it work in C#, we'd have to just spit a whole bunch of little helper methods that do casts on the return type to the right thing. There's nothing stopping you from doing that yourself.

1)CLR不支持它。为了使它在C#中工作,我们必须吐出一大堆小助手方法,这些方法将返回类型转换为正确的东西。没有什么能阻止你自己这样做。

2) Anders believes that return type covariance is not a good language feature.

2)Anders认为返回类型协方差不是一个好的语言特征。

3) \We have lots of higher priorities for the language. We have only limited budgets and so we try to only do the best features we can in any given release. Sure, this would be nice, but it's easy enough to do on your own if you want to. Better that we spend the time adding features that improve the developer experience or add more representational power to the language.

3)\我们有很多更高的语言优先级。我们的预算有限,因此我们只尝试在任何给定的版本中尽我们所能。当然,这会很好,但如果你愿意的话,这很容易就可以自己做。更好的是,我们花时间添加可改善开发人员体验的功能或为语言添加更具代表性的功能。

#2


15  

You could use explicit interface implementation to avoid the problem.

您可以使用显式接口实现来避免此问题。

class  X : IA, IB
{
  public X test()
  {
    return this;
  }

  IB IA.test()
  {
    return this;
  }
}

interface IA
{
  IB test();
}

interface IB
{
}

#3


5  

The signatures must match exactly to what the interface specifies. There's no reason you cannot return an instance of X from the method, but the method signature will have to use IB.

签名必须与接口指定的完全匹配。没有理由不能从方法返回X的实例,但方法签名必须使用IB。

As for a rational reason.. it's probably preferable from a code readability point of view.

至于理性的原因......从代码可读性的角度来看,这可能是最好的。

You could implement the interface explicitly, and provide an alternative signature that returns X that is not defined by the interface. If you know your IA is actually an X, you could use that instead.

您可以显式实现接口,并提供一个替代签名,该签名返回未由接口定义的X.如果您知道您的IA实际上是X,那么您可以使用它。

#4


3  

Because C# does not support co and contravriance for interfaces in compile time. This way an implementation of IA.Test() method must exactly match its declaration. You can, however, return instance of X in runtime

因为C#在编译时不支持接口的co和contravriance。这样,IA.Test()方法的实现必须与其声明完全匹配。但是,您可以在运行时返回X的实例

#5


1  

public X test();

You must declare a body for all methods in any class that's not abstract.

您必须为任何不抽象的类声明所有方法的主体。

Try this:

尝试这个:

class X : IA, IB
{
    public IB test()
    {
        return new X();
    }
}

interface IA
{
    IB test();
}
interface IB { };

#6


1  

This can help http://geekswithblogs.net/abhijeetp/archive/2010/01/10/covariance-and-contravariance-in-c-4.0.aspx You can use the "out" keyword

这可以帮助http://geekswithblogs.net/abhijeetp/archive/2010/01/10/covariance-and-contravariance-in-c-4.0.aspx您可以使用“out”关键字