zoukankan      html  css  js  c++  java
  • .Net 【提高】 协变和逆变

    协变 逆变

    主要用于泛型接口和委托中
    协变就是支持泛型子类自动转泛型父类
    逆变就是支持泛型父类自动转泛型子类

    协变IEnumerable out

    namespace System.Collections.Generic
    {
        //
        // 摘要:
        //     Exposes the enumerator, which supports a simple iteration over a collection of
        //     a specified type.
        //
        // 类型参数:
        //   T:
        //     The type of objects to enumerate.
        public interface IEnumerable<out T> : IEnumerable
        {
            //
            // 摘要:
            //     Returns an enumerator that iterates through the collection.
            //
            // 返回结果:
            //     An enumerator that can be used to iterate through the collection.
            IEnumerator<T> GetEnumerator();
        }
    }
    

    逆变IComparable in

    namespace System
    {
        //
        // 摘要:
        //     Defines a generalized comparison method that a value type or class implements
        //     to create a type-specific comparison method for ordering or sorting its instances.
        //
        // 类型参数:
        //   T:
        //     The type of object to compare.
        public interface IComparable<in T>
        {
            //
            // 摘要:
            //     Compares the current instance with another object of the same type and returns
            //     an integer that indicates whether the current instance precedes, follows, or
            //     occurs in the same position in the sort order as the other object.
            //
            // 参数:
            //   other:
            //     An object to compare with this instance.
            //
            // 返回结果:
            //     A value that indicates the relative order of the objects being compared. The
            //     return value has these meanings: Value Meaning Less than zero This instance precedes
            //     other in the sort order. Zero This instance occurs in the same position in the
            //     sort order as other. Greater than zero This instance follows other in the sort
            //     order.
            int CompareTo(T other);
        }
    }
    

    逆变Action in

    namespace System
    {
        //
        // 摘要:
        //     Encapsulates a method that has a single parameter and does not return a value.
        //
        // 参数:
        //   obj:
        //     The parameter of the method that this delegate encapsulates.
        //
        // 类型参数:
        //   T:
        //     The type of the parameter of the method that this delegate encapsulates.
        public delegate void Action<in T>(T obj);
    }
    

    .NET 中自带的斜变逆变泛型

    IEnumerable<out T> //接口
    Action<in T> //委托
    Func<out TResult> //委托
    IReadonlyList<out T>//接口
    IReadonlycollection<out T>//接口
    
  • 相关阅读:
    C++中的命名空间
    [3D数学基础:图形与游戏开发]专栏前言
    Step01-题目申报
    《通用型云端物联网网关系统的设计与实现》
    博弈论题目总结(一)——简单组合游戏
    单纯形模板
    BZOJ 3434 [WC2014]时空穿梭 (莫比乌斯反演)
    BZOJ 3533 [SDOI2014]向量集 (线段树维护凸包)
    BZOJ 2161 布娃娃 (主席树)
    UOJ #86 mx的组合数 (数位DP+NTT+原根优化)
  • 原文地址:https://www.cnblogs.com/thomerson/p/15073209.html
Copyright © 2011-2022 走看看