• 类型参数的约束


    在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。这些限制称为约束。约束是使用 where 上下文关键字指定的。下表列出了六种类型的约束:

    约束说明

    T:结构

    类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。有关更多信息,请参见使用可空类型(C# 编程指南)

    T:类

    类型参数必须是引用类型,包括任何类、接口、委托或数组类型。

    T:new()

    类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。

    T:<基类名>

    类型参数必须是指定的基类或派生自指定的基类。

    T:<接口名称>

    类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。

    T:U

    为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。

    如果要检查泛型列表中的某个项以确定它是否有效,或者将它与其他某个项进行比较,则编译器必须在一定程度上保证它需要调用的运算符或方法将受到客户端代码可能指定的任何类型参数的支持。这种保证是通过对泛型类定义应用一个或多个约束获得的。例如,基类约束告诉编译器:仅此类型的对象或从此类型派生的对象才可用作类型参数。一旦编译器有了这个保证,它就能够允许在泛型类中调用该类型的方法。约束是使用上下文关键字 where 应用的。下面的代码示例演示可通过应用基类约束添加到 GenericList<T> 类(在泛型介绍(C# 编程指南)中)的功能。

    public class Employee
    {
        private string name;
        private int id;
    
        public Employee(string s, int i)
        {
            name = s;
            id = i;
        }
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
    }
    
    public class GenericList<T> where T : Employee
    {
        private class Node
        {
            private Node next;
            private T data;
    
            public Node(T t)
            {
                next = null;
                data = t;
            }
    
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
    
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }
    
        private Node head;
    
        public GenericList() //constructor
        {
            head = null;
        }
    
        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;
    
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }
    
        public T FindFirstOccurrence(string s)
        {
            Node current = head;
            T t = null;
    
            while (current != null)
            {
                //The constraint enables access to the Name property.
                if (current.Data.Name == s)
                {
                    t = current.Data;
                    break;
                }
                else
                {
                    current = current.Next;
                }
            }
            return t;
        }
    }
     
  • 相关阅读:
    @SuppressWarnings("resource")
    连续根据两个字段排序
    java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 出现错误
    java中数组的定义
    单表(多表需手动创建多个转换)插入,更新数据
    批量处理sql
    查询排序后前5名的信息
    面向对象详细
    Flask-SQLAlchemy
    Dbutils-数据库连接池
  • 原文地址:https://www.cnblogs.com/ylwn817/p/2081338.html
Copyright © 2020-2023  润新知