相关疑难解决方法(0)

我可以用派生类型覆盖吗?

据我所知,在C#2.0中无法执行以下操作

public class Father
{
    public virtual Father SomePropertyName
    {
        get
        {
            return this;
        }
    }
}

public class Child : Father
{
    public override Child SomePropertyName
    {
        get
        {
            return this;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我通过在派生类中将属性创建为"new"来解决问题,但当然这不是多态的.

public new Child SomePropertyName
Run Code Online (Sandbox Code Playgroud)

2.0中有什么解决方案吗?3.5中解决此问题的任何功能如何?

.net c# inheritance covariance

39
推荐指数
5
解决办法
3万
查看次数

为什么接口实现不能返回更具体的类型?

如果接口指定了返回另一个接口的属性或方法,为什么第一个接口的实现不允许将返回类型"更改"为更具体的类型?

我们举一个例子来说明:

interface IFoo
{
    IBar GetBar();
}
interface IBar
{ }

class Foo : IFoo
{
    // This is illegal, we are not implementing IFoo properly
    public Bar GetBar()
    {
        return new Bar();
    }
}

class Bar : IBar
{ }
Run Code Online (Sandbox Code Playgroud)

我知道如何让它发挥作用,这不是我关心的问题.

我可以:

  • 将返回类型更改GetFoo()IBar,或
  • 显式实现接口,只需GetBarIFoo.GetBar()方法调用即可

我真正要问的是不仅允许上面的代码编译的原因.是否有上述情况不符合规定的合同IFoo.

c# overloading language-design interface

31
推荐指数
2
解决办法
2562
查看次数

c#使用泛型的协变返回类型

下面的代码是实现协变返回类型的唯一方法吗?

public abstract class BaseApplication<T> {
    public T Employee{ get; set; }
}

public class Application : BaseApplication<ExistingEmployee> {}

public class NewApplication : BaseApplication<NewEmployee> {}
Run Code Online (Sandbox Code Playgroud)

我希望能够构造一个Application或NewApplication并让它从Employee属性返回适当的Employee类型.

var app = new Application();
var employee = app.Employee; // this should be of type ExistingEmployee
Run Code Online (Sandbox Code Playgroud)

我相信这段代码工作得很好,但是当我有几个需要相同行为的属性时,它变得非常讨厌.

有没有其他方法来实现这种行为?泛型或其他?

c# generics covariance

26
推荐指数
1
解决办法
6140
查看次数

为什么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 { };
Run Code Online (Sandbox Code Playgroud)

.net c#

23
推荐指数
3
解决办法
3179
查看次数

为什么C#不支持变体泛型类?

拿这个小LINQPad示例:

void Main()
{
    Foo<object> foo = new Foo<string>();
    Console.WriteLine(foo.Get());
}

class Foo<out T>
{
    public T Get()
    {
        return default(T);
    }
}
Run Code Online (Sandbox Code Playgroud)

它无法使用此错误进行编译:

方差修饰符无效.只能将接口和委托类型参数指定为变量.

我没有看到代码的任何逻辑问题.一切都可以静态验证.为什么不允许这样做?它是否会导致语言不一致,或者由于CLR的限制而被认为实施起来太昂贵了?如果是后者,我作为开发人员应该知道什么是上述限制?

考虑到接口支持它,我希望从逻辑上遵循该类支持.

c# generics class generic-variance

19
推荐指数
1
解决办法
1680
查看次数

Func <>的List <>,使用泛型返回类型编译错误,但为什么呢?

这是一个冗长的问题,所以请耐心等待.

我需要在一组字符串和每个字符串的相应泛型方法调用之间创建一个映射.但是我遇到了一个编译问题,向下解释.

在我的场景中,我正在使用a Dictionary<>,但问题同样存在于a List<>.为简单起见,我List<>在下面的示例中使用了a .

考虑这三个类:

public abstract class MyBase { /* body omitted */  }
public class MyDerived1 : MyBase { /* body omitted */  }
public class MyDerived2 : MyBase { /* body omitted */  }
Run Code Online (Sandbox Code Playgroud)

还有一些其他类的方法:

public class Test
{
    public T GetT<T>() where T : MyBase { /* body omitted */ }
}
Run Code Online (Sandbox Code Playgroud)

在另一个课程中,我可以声明List<Func<MyBase>>如下:

public class SomeClass
{
    public void SomeFunc()
    {
        var test = new Test();

        var list1 …
Run Code Online (Sandbox Code Playgroud)

c# generics .net-4.0 func

16
推荐指数
2
解决办法
3437
查看次数

为什么我不能这样实现接口?

可能重复:
C#是否支持返回类型协方差?

我不确定我是不是真的很蠢......

如果我有一个界面:

public interface IMoop
{
    object Moop();
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样实现它(我猜这会使用隐式协方差?)

public class MoopImplementor : IMoop
{
    string Moop();
}
Run Code Online (Sandbox Code Playgroud)

MoopImplementor的任何实例都符合IMoop指定的合同,所以看起来这应该没问题.

请赐教:)

编辑:要清楚 - 因为实现类返回继承自Interfaced方法的返回类型的东西 - 我觉得这应该工作.具体来说,是一个stringIS object.(对于任何其他的承保链也是如此).

c#

12
推荐指数
2
解决办法
625
查看次数

使用通用约束实现接口

有点惊讶为什么这不起作用

这是编译器的限制还是不支持它是否合理?

public class Class1<T> : IInterface
    where T : Test2
{
    public T Test { get; private set; }
}

public class Test2
{
}

internal interface IInterface
{
    Test2 Test { get; }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

'ClassLibrary1.Class1<T>' does not implement interface member 'ClassLibrary1.IInterface.Test'. 
'ClassLibrary1.Class1<T>.Test' cannot implement 'ClassLibrary1.IInterface.Test' because it does not have the matching return type of 'ClassLibrary1.Test2'.
Run Code Online (Sandbox Code Playgroud)

c# generics

11
推荐指数
2
解决办法
4893
查看次数

C#中的协方差有哪些?(或者,协方差:例如)

协方差(大致)是在使用它们的复杂类型中镜像 "简单"类型的继承的能力.
例如,我们总是可以将一个实例Cat视为一个实例Animal.如果ComplexType是协变ComplexType<Cat>ComplexType<Animal>,则可以将A 视为a .

我想知道:协方差的"类型"是什么,它们与C#有什么关系(它们是否受支持?)
代码示例会有所帮助.

例如,一种类型是返回类型协方差,由Java支持,但不支持C#.

我希望有功能编程印章的人也可以加入!

c# java functional-programming covariance

10
推荐指数
1
解决办法
1451
查看次数

接口实现上成员的返回类型必须与下层定义完全​​匹配?

根据CSharp语言规范.

接口定义了可以通过类和结构实现的契约.接口不提供它定义的成员的实现 - 它仅指定必须由实现接口的类或结构提供的成员.

所以我有这个:

interface ITest
{
    IEnumerable<int> Integers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的意思是."我与一个属性的合同是一个你可以枚举的整数集合".

然后我想要以下接口实现:

class Test : ITest
{
    public List<int> Integers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我得到以下编译器错误:

'Test'没有实现接口成员'ITest.Integers'.'Test.Integers'无法实现'ITest.Integers',因为它没有匹配的返回类型'System.Collections.Generic.IEnumerable'.

只要我可以说我的Test类实现了ITest契约,因为int属性的List实际上是int的IEnumerable.

那么c#编译器告诉我错误的方式呢?

c#

9
推荐指数
3
解决办法
4315
查看次数