covariance

Override a Property with a Derived Type and Same Name C#

╄→гoц情女王★ 提交于 2019-12-31 14:19:02
问题 I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it? The following code gets the error: Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache' public class OuterSpace { public virtual OuterSpaceData Data {get; set;} public virtual OuterSpaceAnalysis Analysis {get; set;} public virtual OuterSpaceCache Cache {get; set;}

Override a Property with a Derived Type and Same Name C#

感情迁移 提交于 2019-12-31 14:16:06
问题 I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it? The following code gets the error: Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache' public class OuterSpace { public virtual OuterSpaceData Data {get; set;} public virtual OuterSpaceAnalysis Analysis {get; set;} public virtual OuterSpaceCache Cache {get; set;}

Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?

吃可爱长大的小学妹 提交于 2019-12-31 02:53:09
问题 Following on from this question, can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // "COVARIANT parameter in CONTRAVARIANT position" } I understand the distinction between +T and T in the type declaration (it compiles if I use T ). But then how does one actually write a class which is covariant in its type parameter without resorting to creating the thing unparametrized ? How can I ensure that the following can only be created with an instance of

Java covariant array bad?

≯℡__Kan透↙ 提交于 2019-12-30 10:33:12
问题 I've been told by several people that Java allows covariant array subtyping in other words if A is a subtype of B, then A[] is a subtype of B[], but that this is a bad feature because it can lead to runtime errors. Can someone give me a concrete example to illustrate how it causes runtime errors and if/how does Java address this problem? Thank you! 回答1: Very simple. String strings[] = {"Broken","Type", "system"}; Object objects[] = strings; objects[0] = 5; // compiles fine, but throws

.NET equivalent for Java wildcard generics <?> with co- and contra- variance?

余生颓废 提交于 2019-12-30 09:59:08
问题 I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. For instance: Java: interface IInterf { } class Impl implements IInterf { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); void method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val); } abstract class Generic<T extends Impl>

Derived type of generic base class

谁说我不能喝 提交于 2019-12-30 07:11:26
问题 I have the following code. class Header<T> where T: IItem { } class HeaderA : Header<ItemA> { } class HeaderB : Header<ItemB> { } interface IItem { } class ItemA : IItem { } class ItemB : IItem { } Header<IItem> h = new HeaderA(); The last line cannot be compiled. Cannot implicitly convert type 'UserQuery.HeaderA' to 'UserQuery.Header<UserQuery.IItem>' HeaderA is a subtype of Header and ItemA is a subtype of IItem. Why it doesn't work? 回答1: In short, you're trying to use a concept called

Derived type of generic base class

99封情书 提交于 2019-12-30 07:11:09
问题 I have the following code. class Header<T> where T: IItem { } class HeaderA : Header<ItemA> { } class HeaderB : Header<ItemB> { } interface IItem { } class ItemA : IItem { } class ItemB : IItem { } Header<IItem> h = new HeaderA(); The last line cannot be compiled. Cannot implicitly convert type 'UserQuery.HeaderA' to 'UserQuery.Header<UserQuery.IItem>' HeaderA is a subtype of Header and ItemA is a subtype of IItem. Why it doesn't work? 回答1: In short, you're trying to use a concept called

F# and interface covariance: what to do? (specifically seq<> aka IEnumerable<>)

99封情书 提交于 2019-12-30 05:59:25
问题 I'm trying to call a .NET method accepting a generic IEnumerable<T> from F# using a seq<U> such that U is a subclass of T. This doesn't work the way I expected it would: With the following simple printer: let printEm (os: seq<obj>) = for o in os do o.ToString() |> printfn "%s" These are the results I get: Seq.singleton "Hello World" |> printEm // error FS0001; //Expected seq<string> -> 'a but given seq<string> -> unit Seq.singleton "Hello World" :> seq<obj> |> printEm // error FS0193; //seq

Overriding abstract property using more specified return type (covariance)

假装没事ソ 提交于 2019-12-29 09:08:11
问题 class Base {} abstract class A { abstract public List<Base> Items { get; set; } } class Derived : Base {} class B : A { private List<Derived> items; public override List<Derived> Items { get { return items; } set { items = value; } } } The compiler says that B.Items must be List of Base elements "to match overridden member" A.Items. How can i make that work? 回答1: What you've tried to accomplish initially is impossible - .NET does not support co(contra)variance for method overload. The same

How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues

China☆狼群 提交于 2019-12-29 01:34:14
问题 I am trying to combine (union or concat) two lists/collection into one. The two lists have a common base class. e.g. I've tried this: IQueryable<ContractItem> contractItems = myRepository.RetrieveContractItems(); IQueryable<ChangeOrderItem> changeOrderItems = myRepository.RetrieveChangeOrderItems(); IQueryable<ItemBase> folderItems = contractItems.Concat<ItemBase>(changeOrderItems); But am getting the LINQ error DbUnionAllExpression requires arguments with compatible collection ResultTypes.