contravariance

    9熱度

    2回答

    我收到以下錯誤: ClassName.PropertyName cannot implement IClassType.PropertyName because it does not have the matching return type of IBasePropertyType 現在,代碼: public class ClassName : IClassType { publi

    2熱度

    3回答

    是否有可能以某種方式實現類模板,即如果一個對象的模板參數相關,則可以將其轉換爲另一個對象?這裏是一個的exaple顯示的想法(當然它不會編譯): struct Base {}; struct Derived : Base {}; template <typename T> class Foo { virtual ~Foo() {} virtual T* some_func

    14熱度

    3回答

    考慮以下幾種類型: public interface IMyClass { } public class MyClass : IMyClass { } 我不知道如何可以轉換List<MyClass>到List<IMyClass>?我並不完全清楚協變性/逆變性話題,但是我明白我不能因爲這個而清楚地列出這個列表。 我可以想出這個微不足道的解決方案;缺乏任何高雅,浪費資源: ... public

    1熱度

    2回答

    似乎C#4.0不支持覆蓋參數中的協方差(使用「in」關鍵字);是這樣嗎? 如果是這樣,有沒有更好的方法來做到這一點? 背景 public interface IBaseEvent { /* ... */ } public interface IDerivedEvent : IBaseEvent { /* ... */ } public class MoreDerivedEvent : ID

    31熱度

    3回答

    這是什麼問題? interface IRepository<out T> where T : IBusinessEntity { IQueryable<T> GetAll(); void Save(T t); void Delete(T t); } 它說: 無效方差:類型參數 'T' 必須是 'MyNamespace.IRepository.Delete(T)

    2熱度

    2回答

    我有一個類中聲明是這樣的: internal private abstract class BoxGroup<TS> : IBoxGroup where TS : SavedState 那堂課我有這樣的方法: protected virtual TS saveState() { return new SavedState(Width, Height); } 我認爲這將是正確的,

    2熱度

    4回答

    如何和爲什麼「VAL」和「案例」影響類型系統? (尤其是方差) Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22). Type in expressions to have them evaluated. Type :help for more information.

    3熱度

    1回答

    這是我第一次在這裏發表問題,所以先感謝任何人都會幫助我(也許還有其他人)。 今天我一直不解帶協方差和逆變在C#4,但我實在看不出任何可能性做什麼,我會的。 注:所有這些都是不來解決特定的問題,但我想知道是否有任何合法的方式(全新CO /禁忌方差在C#4),以解決這個問題。 所以,不要弄瘋了... 下面的代碼示例: public interface IBase<in T> { T Get

    1熱度

    2回答

    可以說我有以下的階層結構在我的數據訪問層: interface IBehavior<in T> { void Load(T model); } class ModelManager<T> { ModelManager(IEnumerable<IBehavior<T>> behaviors) { ... } void Load(T model) {

    3熱度

    2回答

    從Wikipedia: 協變:從更寬的(雙)轉化成更窄(浮點)。 contravariant:從較窄(浮動)轉換爲較寬(雙)。 在.NET中,委託具有協方差,因爲它允許派生類型委託的指定的返回類型是其持有的基準的方法的返回類型。 除了一個代表具有逆變因爲它允許派生類型的代理的指定的參數(參數)要傳遞到它持有到基準的方法的參數的類型。 有了這兩個定義,因爲它們與代表有關,它們不應該都是協方差?在均爲