柚子快报激活码778899分享:C# 泛型

http://www.51969.com/

为什么要有泛型:

我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类型不同。

你可能会想到用object,来解决这个问题。但是,缺陷的: 1.会出现装箱、折箱操作,这将在托管堆上分配和回收大量的变量,若数据量大,则性能损失非常严重。 2.在处理引用类型时,虽然没有装箱和折箱操作,但将用到数据类型的强制转换操作,增加处理器的负担。 

介绍泛型:

C#中的泛型能够将类型作为参数来传递,即在创建类型时用一个特定的符号如“T”来作为一个占位符,代替实际的类型,等待在实例化时用一个实际的类型来代替。

泛型的优点 :

一、使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。 二、降低了强制转换或装箱操作的成本或风险。 三、可以对泛型类进行约束以访问特定数据类型的方法。

泛型的限制:

void MyFunc(T x, T y) { x = x + y; //报错!泛型的变量不能使用运算符’+’ }

泛型成员因类型不确定,可能是类、结构体、字符、枚举……所以不能使用算术运算符、比较运算符等进行运算!注意,可以使用赋值运算符。 

泛型类型参数:

在泛型类型或方法定义中,类型参数是客户端在实例化泛型类型的变量时指定的特定类型的占位符。

注意: 1.类型参数并不是只有一个,可以有多个。 2.类型参数可以是编译器识别的任何类型。 3.类型参数的名字不能随便起,不能重名。

类型参数的约束: 

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

下表列出了各种类型的约束:

约束描述where T : struct类型参数必须是不可为 null 的值类型。  由于所有值类型都具有可访问的无参数构造函数,因此 struct 约束表示 new() 约束,并且不能与 new() 约束结合使用。 struct 约束也不能与 unmanaged 约束结合使用。where T : class类型参数必须是引用类型。 此约束还应用于任何类、接口、委托或数组类型。 在 C#8.0 或更高版本中的可为 null 上下文中,T 必须是不可为 null 的引用类型。where T : class?类型参数必须是可为 null 或不可为 null 的引用类型。 此约束还应用于任何类、接口、委托或数组类型。where T : notnull类型参数必须是不可为 null 的类型。 参数可以是 C# 8.0 或更高版本中的不可为 null 的引用类型,也可以是不可为 null 的值类型。where T : default重写方法或提供显式接口实现时,如果需要指定不受约束的类型参数,此约束可解决歧义。 default 约束表示基方法,但不包含 class 或 struct 约束。where T : unmanaged类型参数必须是不可为 null 的非托管类型。 unmanaged 约束表示 struct 约束,且不能与 struct 约束或 new() 约束结合使用。where T : new()类型参数必须具有公共无参数构造函数。 与其他约束一起使用时,new() 约束必须最后指定。 new() 约束不能与 struct 和 unmanaged 约束结合使用。where T : 类型参数必须是指定的基类或派生自指定的基类。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 必须是从指定基类派生的不可为 null 的引用类型。where T : ?类型参数必须是指定的基类或派生自指定的基类。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 可以是从指定基类派生的可为 null 或不可为 null 的类型。where T : 类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 必须是实现指定接口的不可为 null 的类型。where T : ?类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。 在 C# 8.0 中的可为 null 上下文中,T 可以是可为 null 的引用类型、不可为 null 的引用类型或值类型。 T 不能是可为 null 的值类型。where T : U为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。 在可为 null 的上下文中,如果 U 是不可为 null 的引用类型,T 必须是不可为 null 的引用类型。 如果 U 是可为 null 的引用类型,则 T 可以是可为 null 的引用类型,也可以是不可为 null 的引用类型。

使用约束的原因:

 约束指定类型参数的功能和预期。 声明这些约束意味着你可以使用约束类型的操作和方法调用。 如果泛型类或方法对泛型成员使用除简单赋值之外的任何操作或调用 System.Object 不支持的任何方法,则将对类型参数应用约束。 例如,基类约束告诉编译器,仅此类型的对象或派生自此类型的对象可用作类型参数。 编译器有了此保证后,就能够允许在泛型类中调用该类型的方法。 以下代码示例演示可通过应用基类约束添加到(泛型介绍中的)GenericList 类的功能。

public class Employee

{

public Employee(string name, int id) => (Name, ID) = (name, id);

public string Name { get; set; }

public int ID { get; set; }

}

public class GenericList where T : Employee

{

private class Node

{

public Node(T t) => (Next, Data) = (null, t);

public Node Next { get; set; }

public T Data { get; set; }

}

private Node head;

public void AddHead(T t)

{

Node n = new Node(t) { Next = head };

head = n;

}

public IEnumerator 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;

}

}

约束使泛型类能够使用 Employee.Name 属性。 约束指定类型 T 的所有项都保证是 Employee 对象或从 Employee 继承的对象。

可以对同一类型参数应用多个约束,并且约束自身可以是泛型类型,如下所示:

class EmployeeList where T : Employee, IEmployee, System.IComparable, new()

{

// ...

}

 在应用 where T : class 约束时,请避免对类型参数使用 == 和 != 运算符,因为这些运算符仅测试引用标识而不测试值相等性。 即使在用作参数的类型中重载这些运算符也会发生此行为。 下面的代码说明了这一点;即使 String 类重载 == 运算符,输出也为 false。

public static void OpEqualsTest(T s, T t) where T : class

{

System.Console.WriteLine(s == t);

}

private static void TestStringEquality()

{

string s1 = "target";

System.Text.StringBuilder sb = new System.Text.StringBuilder("target");

string s2 = sb.ToString();

OpEqualsTest(s1, s2);

}

编译器只知道 T 在编译时是引用类型,并且必须使用对所有引用类型都有效的默认运算符。 如果必须测试值相等性,建议同时应用 where T : IEquatable 或 where T : IComparable 约束,并在用于构造泛型类的任何类中实现该接口。

约束多个参数 :

可以对多个参数应用多个约束,对一个参数应用多个约束,如下例所示:

class Base { }

class Test

where U : struct

where T : Base, new()

{ }

未绑定的类型参数:

没有约束的类型参数(如公共类 SampleClass{} 中的 T)称为未绑定的类型参数。 未绑定的类型参数具有以下规则:

不能使用 != 和 == 运算符,因为无法保证具体的类型参数能支持这些运算符。可以在它们与 System.Object 之间来回转换,或将它们显式转换为任何接口类型。可以将它们与 null 进行比较。 将未绑定的参数与 null 进行比较时,如果类型参数为值类型,则该比较将始终返回 false。

 类型参数作为约束:

在具有自己类型参数的成员函数必须将该参数约束为包含类型的类型参数时,将泛型类型参数用作约束非常有用,如下例所示:

public class List

{

public void Add(List items) where U : T {/*...*/}

}

在上述示例中,T 在 Add 方法的上下文中是一个类型约束,而在 List 类的上下文中是一个未绑定的类型参数。

类型参数还可在泛型类定义中用作约束。 必须在尖括号中声明该类型参数以及任何其他类型参数:

//Type parameter V is used as a type constraint.

public class SampleClass where T : V { }

 类型参数作为泛型类的约束的作用非常有限,因为编译器除了假设类型参数派生自 System.Object 以外,不会做其他任何假设。 如果要在两个类型参数之间强制继承关系,可以将类型参数用作泛型类的约束。

notnull 约束:

从 C# 8.0 开始,可以使用 notnull 约束指定类型参数必须是不可为 null 的值类型或不可为 null 的引用类型。 与大多数其他约束不同,如果类型参数违反 notnull 约束,编译器会生成警告而不是错误。

notnull 约束仅在可为 null 上下文中使用时才有效。 如果在过时的可为 null 上下文中添加 notnull 约束,编译器不会针对违反约束的情况生成任何警告或错误。

class 约束:

从 C# 8.0 开始,可为 null 上下文中的 class 约束指定类型参数必须是不可为 null 的引用类型。 在可为 null 上下文中,当类型参数是可为 null 的引用类型时,编译器会生成警告。

default 约束:

添加可为空引用类型会使泛型类型或方法中的 T? 使用复杂化。 在 C# 8 之前,只能在向 T 应用了 struct 约束的情况下使用 T?。 在该上下文中,T? 引用 T 的 Nullable 类型。 从 C# 8 开始,T? 可以与 struct 或 class 约束一起使用,但必须存在其中一项。 使用 class 约束时,T? 引用了 T 的可为空引用类型。 从 C# 9 开始,可在这两个约束均未应用时使用 T?。 在这种情况下,T? 的解读与在 C# 8 中对值类型和引用类型的解读相同。 但是,如果 T 是 Nullable的实例,则 T? 与 T 相同。 换句话说,它不会成为 T??。

由于现在可在没有 class 或 struct 约束的情况下使用 T?,因此在重写或显式接口实现中可能会出现歧义。 在这两种情况下,重写不包含约束,但从基类继承。 当基类不应用 class 或 struct 约束时,派生类需要通过某种方式在不使用任一种约束的情况下指定应用于基方法的重写。 此时派生方法将应用 default 约束。 default 约束不阐明 class 和 struct 约束。

非托管约束:

从 C# 7.3 开始,可使用 unmanaged 约束来指定类型参数必须是不可为 null 的非托管类型。 通过 unmanaged 约束,用户能编写可重用例程,从而使用可作为内存块操作的类型,如以下示例所示: 

unsafe public static byte[] ToByteArray(this T argument) where T : unmanaged

{

var size = sizeof(T);

var result = new Byte[size];

Byte* p = (byte*)&argument;

for (var i = 0; i < size; i++)

result[i] = *p++;

return result;

}

以上方法必须在 unsafe 上下文中编译,因为它并不是在已知的内置类型上使用 sizeof 运算符。 如果没有 unmanaged 约束,则 sizeof 运算符不可用。

unmanaged 约束表示 struct 约束,且不能与其结合使用。 因为 struct 约束表示 new() 约束,且 unmanaged 约束也不能与 new() 约束结合使用。

委托约束:

同样从 C# 7.3 开始,可将 System.Delegate 或 System.MulticastDelegate 用作基类约束。 CLR 始终允许此约束,但 C# 语言不允许。 使用 System.Delegate 约束,用户能够以类型安全的方式编写使用委托的代码。 以下代码定义了合并两个同类型委托的扩展方法: 

public static TDelegate TypeSafeCombine(this TDelegate source, TDelegate target)

where TDelegate : System.Delegate

=> Delegate.Combine(source, target) as TDelegate;

可使用上述方法来合并相同类型的委托:

Action first = () => Console.WriteLine("this");

Action second = () => Console.WriteLine("that");

var combined = first.TypeSafeCombine(second);

combined();

Func test = () => true;

// Combine signature ensures combined delegates must

// have the same type.

//var badCombined = first.TypeSafeCombine(test);

 如果取消评论最后一行,它将不会编译。 first 和 test 均为委托类型,但它们是不同的委托类型。

枚举约束:

从 C# 7.3 开始,还可指定 System.Enum 类型作为基类约束。 CLR 始终允许此约束,但 C# 语言不允许。 使用 System.Enum 的泛型提供类型安全的编程,缓存使用 System.Enum 中静态方法的结果。 以下示例查找枚举类型的所有有效的值,然后生成将这些值映射到其字符串表示形式的字典。

public static Dictionary EnumNamedValues() where T : System.Enum

{

var result = new Dictionary();

var values = Enum.GetValues(typeof(T));

foreach (int item in values)

result.Add(item, Enum.GetName(typeof(T), item));

return result;

}

Enum.GetValues 和 Enum.GetName 使用反射,这会对性能产生影响。 可调用 EnumNamedValues 来生成可缓存和重用的集合,而不是重复执行需要反射才能实施的调用。

如以下示例所示,可使用它来创建枚举并生成其值和名称的字典:

enum Rainbow

{

Red,

Orange,

Yellow,

Green,

Blue,

Indigo,

Violet

}

var map = EnumNamedValues();

foreach (var pair in map)

Console.WriteLine($"{pair.Key}:\t{pair.Value}");

类型参数命名指南 :

1,请使用描述性名称命名泛型类型参数,除非单个字母名称完全具有自我说明性且描述性名称不会增加任何作用。

public interface ISessionChannel { /*...*/ }

public delegate TOutput Converter(TInput from);

public class List { /*...*/ }

2,对具有单个字母类型参数的类型,考虑使用 T 作为类型参数名称。

public int IComparer() { return 0; }

public delegate bool Predicate(T item);

public struct Nullable where T : struct { /*...*/ }

 3,在类型参数描述性名称前添加前缀 "T"。

public interface ISessionChannel

{

TSession Session { get; }

}

 4,请考虑在参数名称中指示出类型参数的约束。

泛型类:

泛型类封装不特定于特定数据类型的操作。 泛型类最常见用法是用于链接列表、哈希表、堆栈、队列和树等集合。 无论存储数据的类型如何,添加项和从集合删除项等操作的执行方式基本相同。

对于大多数需要集合类的方案,推荐做法是使用 .NET 类库中提供的集合类。

通常,创建泛型类是从现有具体类开始,然后每次逐个将类型更改为类型参数,直到泛化和可用性达到最佳平衡。 创建自己的泛型类时,需要考虑以下重要注意事项:

要将哪些类型泛化为类型参数。 通常,可参数化的类型越多,代码就越灵活、其可重用性就越高。 但过度泛化会造成其他开发人员难以阅读或理解代码。 要将何种约束(如有)应用到类型参数 其中一个有用的规则是,应用最大程度的约束,同时仍可处理必须处理的类型。 例如,如果知道泛型类仅用于引用类型,则请应用类约束。 这可防止将类意外用于值类型,并使你可在 T 上使用 as 运算符和检查 null 值。 是否将泛型行为分解为基类和子类。 因为泛型类可用作基类,所以非泛型类的相同设计注意事项在此也适用。 请参阅本主题后文有关从泛型基类继承的规则。 实现一个泛型接口还是多个泛型接口。 例如,如果要设计用于在基于泛型的集合中创建项的类,则可能必须实现一个接口,例如 IComparable,其中 T 为类的类型。

创建泛型类的过程为:从一个现有的具体类开始,逐一将每个类型更改为类型参数,直至达到通用化和可用性的最佳平衡。 

类型参数和约束的规则对于泛型类行为具有多种含义,尤其是在继承性和成员可访问性方面。 应当了解一些术语,然后再继续。 对于泛型类 Node,客户端代码可通过指定类型参数来引用类,创建封闭式构造类型 (Node)。 或者,可以不指定类型参数(例如指定泛型基类时),创建开放式构造类型 (Node)。 泛型类可继承自具体的封闭式构造或开放式构造基类:

class BaseNode { }

class BaseNodeGeneric { }

// concrete type

class NodeConcrete : BaseNode { }

//closed constructed type

class NodeClosed : BaseNodeGeneric { }

//open constructed type

class NodeOpen : BaseNodeGeneric { }

非泛型类(即,具体类)可继承自封闭式构造基类,但不可继承自开放式构造类或类型参数,因为运行时客户端代码无法提供实例化基类所需的类型参数。

//No error

class Node1 : BaseNodeGeneric { }

//Generates an error

//class Node2 : BaseNodeGeneric {}

//Generates an error

//class Node3 : T {}

继承自开放式构造类型的泛型类必须对非此继承类共享的任何基类类型参数提供类型参数,如下方代码所示:

class NodeItem where T : System.IComparable, new() { }

class SpecialNodeItem : NodeItem where T : System.IComparable, new() { }

泛型类型可使用多个类型参数和约束,如下所示:

class SuperKeyType

where U : System.IComparable

where V : new()

{ }

开放式构造和封闭式构造类型可用作方法参数:

void Swap(List list1, List list2)

{

//code to swap items

}

void Swap(List list1, List list2)

{

//code to swap items

}

如果一个泛型类实现一个接口,则该类的所有实例均可强制转换为该接口。

泛型类是不变量。 换而言之,如果一个输入参数指定 List,且你尝试提供 List,则会出现编译时错误。

 学习目标:

为泛型集合类或表示集合中的项的泛型类定义接口通常很有用处。 为避免对值类型的装箱和取消装箱操作,泛型类的首选项使用泛型接口,例如 IComparable而不是 IComparable。

接口被指定为类型参数上的约束时,仅可使用实现接口的类型。 如下代码示例演示一个派生自 GenericList 类的 SortedList 类。 有关详细信息,请参阅泛型介绍。 SortedList 添加约束 where T : IComparable。 这可使 SortedList 中的 BubbleSort 方法在列表元素上使用泛型 CompareTo 方法。 在此示例中,列表元素是一个实现 IComparable 的简单类 Person。

//Type parameter T in angle brackets.

public class GenericList : System.Collections.Generic.IEnumerable

{

protected Node head;

protected Node current = null;

// Nested class is also generic on T

protected class Node

{

public Node next;

private T data; //T as private member datatype

public Node(T t) //T used in non-generic constructor

{

next = null;

data = t;

}

public Node Next

{

get { return next; }

set { next = value; }

}

public T Data //T as return type of property

{

get { return data; }

set { data = value; }

}

}

public GenericList() //constructor

{

head = null;

}

public void AddHead(T t) //T as method parameter type

{

Node n = new Node(t);

n.Next = head;

head = n;

}

// Implementation of the iterator

public System.Collections.Generic.IEnumerator GetEnumerator()

{

Node current = head;

while (current != null)

{

yield return current.Data;

current = current.Next;

}

}

// IEnumerable inherits from IEnumerable, therefore this class

// must implement both the generic and non-generic versions of

// GetEnumerator. In most cases, the non-generic method can

// simply call the generic method.

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return GetEnumerator();

}

}

public class SortedList : GenericList where T : System.IComparable

{

// A simple, unoptimized sort algorithm that

// orders list elements from lowest to highest:

public void BubbleSort()

{

if (null == head || null == head.Next)

{

return;

}

bool swapped;

do

{

Node previous = null;

Node current = head;

swapped = false;

while (current.next != null)

{

// Because we need to call this method, the SortedList

// class is constrained on IComparable

if (current.Data.CompareTo(current.next.Data) > 0)

{

Node tmp = current.next;

current.next = current.next.next;

tmp.next = current;

if (previous == null)

{

head = tmp;

}

else

{

previous.next = tmp;

}

previous = tmp;

swapped = true;

}

else

{

previous = current;

current = current.next;

}

}

} while (swapped);

}

}

// A simple class that implements IComparable using itself as the

// type argument. This is a common design pattern in objects that

// are stored in generic lists.

public class Person : System.IComparable

{

string name;

int age;

public Person(string s, int i)

{

name = s;

age = i;

}

// This will cause list elements to be sorted on age values.

public int CompareTo(Person p)

{

return age - p.age;

}

public override string ToString()

{

return name + ":" + age;

}

// Must implement Equals.

public bool Equals(Person p)

{

return (this.age == p.age);

}

}

public class Program

{

public static void Main()

{

//Declare and instantiate a new generic SortedList class.

//Person is the type argument.

SortedList list = new SortedList();

//Create name and age values to initialize Person objects.

string[] names = new string[]

{

"Franscoise",

"Bill",

"Li",

"Sandra",

"Gunnar",

"Alok",

"Hiroyuki",

"Maria",

"Alessandro",

"Raul"

};

int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };

//Populate the list.

for (int x = 0; x < 10; x++)

{

list.AddHead(new Person(names[x], ages[x]));

}

//Print out unsorted list.

foreach (Person p in list)

{

System.Console.WriteLine(p.ToString());

}

System.Console.WriteLine("Done with unsorted list");

//Sort the list.

list.BubbleSort();

//Print out sorted list.

foreach (Person p in list)

{

System.Console.WriteLine(p.ToString());

}

System.Console.WriteLine("Done with sorted list");

}

}

可将多个接口指定为单个类型上的约束,如下所示:

class Stack where T : System.IComparable, IEnumerable

{

}

 一个接口可定义多个类型参数,如下所示:

interface IDictionary

{

}

适用于类的继承规则也适用于接口:

interface IMonth { }

interface IJanuary : IMonth { } //No error

interface IFebruary : IMonth { } //No error

interface IMarch : IMonth { } //No error

//interface IApril : IMonth {} //Error

 如果泛型接口是协变的(即,仅使用自身的类型参数作为返回值),那么这些接口可继承自非泛型接口。 

具体类可实现封闭式构造接口,如下所示:

interface IBaseInterface { }

class SampleClass : IBaseInterface { }

只要类形参列表提供接口所需的所有实参,泛型类即可实现泛型接口或封闭式构造接口,如下所示:

interface IBaseInterface1 { }

interface IBaseInterface2 { }

class SampleClass1 : IBaseInterface1 { } //No error

class SampleClass2 : IBaseInterface2 { } //No error

 控制方法重载的规则对泛型类、泛型结构或泛型接口内的方法一样。

泛型方法:

泛型方法是通过类型参数声明的方法,如下所示:

static void Swap(ref T lhs, ref T rhs)

{

T temp;

temp = lhs;

lhs = rhs;

rhs = temp;

}

类型推理的相同规则适用于静态方法和实例方法。 编译器可基于传入的方法参数推断类型参数;而无法仅根据约束或返回值推断类型参数。 因此,类型推理不适用于不具有参数的方法。 类型推理发生在编译时,之后编译器尝试解析重载的方法签名。 编译器将类型推理逻辑应用于共用同一名称的所有泛型方法。 在重载解决方案步骤中,编译器仅包含在其上类型推理成功的泛型方法。 

 

柚子快报激活码778899分享:C# 泛型

http://www.51969.com/

查看原文