据我所知,在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中解决此问题的任何功能如何?
如果接口指定了返回另一个接口的属性或方法,为什么第一个接口的实现不允许将返回类型"更改"为更具体的类型?
我们举一个例子来说明:
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,或GetBar从IFoo.GetBar()方法调用即可我真正要问的是不仅允许上面的代码编译的原因.是否有上述情况不符合规定的合同IFoo.
下面的代码是实现协变返回类型的唯一方法吗?
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#中不合法?
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) 拿这个小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的限制而被认为实施起来太昂贵了?如果是后者,我作为开发人员应该知道什么是上述限制?
考虑到接口支持它,我希望从逻辑上遵循该类支持.
这是一个冗长的问题,所以请耐心等待.
我需要在一组字符串和每个字符串的相应泛型方法调用之间创建一个映射.但是我遇到了一个编译问题,向下解释.
在我的场景中,我正在使用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#是否支持返回类型协方差?
我不确定我是不是真的很蠢......
如果我有一个界面:
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.(对于任何其他的承保链也是如此).
有点惊讶为什么这不起作用
这是编译器的限制还是不支持它是否合理?
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) 协方差(大致)是在使用它们的复杂类型中镜像 "简单"类型的继承的能力.
例如,我们总是可以将一个实例Cat视为一个实例Animal.如果ComplexType是协变ComplexType<Cat>的ComplexType<Animal>,则可以将A 视为a .
我想知道:协方差的"类型"是什么,它们与C#有什么关系(它们是否受支持?)
代码示例会有所帮助.
例如,一种类型是返回类型协方差,由Java支持,但不支持C#.
我希望有功能编程印章的人也可以加入!
根据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# ×10
generics ×4
covariance ×3
.net ×2
.net-4.0 ×1
class ×1
func ×1
inheritance ×1
interface ×1
java ×1
overloading ×1