泛型类型

概念: 类,结构,接口都可以是泛型的,泛型类型有类型形参,它们是以后要完成的占位符。

(例如 泛型类仍然是类 只不过是带类型形参而已)


泛型类型也称为构造类型 有开放式的构造类型和封闭式构造类型

开方式的构造类型至少有一个类型形参是一个占位符,不收具体类型的约束

(如 public class teacher<T>{})



如果泛型方法和非泛型方法的组合有歧义 则调用非泛型的方法。



封装一个具有一个参数并返回TResult参数指定类型的方法
public delegate TResult Func<in T, out TResult>(
T arg
)




Func<T, TResult>泛型委托

封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。

命名空间: System
程序集: System.Core(在 System.Core.dll 中)

语法
public delegate TResult Func<T, TResult>(
T arg
)
类型参数
T
此委托封装的方法的参数类型。

TResult
此委托封装的方法的返回值类型。

参数
arg
类型:T

此委托封装的方法的参数。

返回值
类型:TResult

此委托封装的方法的返回值。

备注
可以使用此委托构造一个能以参数形式传递的方法,而不用显式声明自定义的委托。该方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且必须返回值。

注意:
若要引用具有一个参数并返回 void 的方法(或者要在 Visual Basic 中引用被声明为 Sub 而不是被声明为 Function 的方法),请改用泛型 Action<T> 委托。

在使用 Func<T, TResult>委托时,不必显式定义一个封装只有一个参数的方法的委托。例如,以下代码显式声明了一个名为 ConvertMethod 的委托,并将对 UppercaseString 方法的引用分配给其委托实例。


using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMeth(name));
}

private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}

以下示例简化了此代码,它所用的方法是实例化 Func<T, TResult> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

using System;

public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLi

ne(convertMethod(name));
}

private static string UppercaseString(string inputString)
{
return inputString.ToUpper
();
}
}

您也可以按照以下示例所演示的那样在 C# 中将 Func<T, TResult> 委托与匿名方法一起使用。(有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)
using System;

public class Anonymous
{
public static void Main()
{
Func<string, string> convert = delegate(string s)
{ return s.ToUpper();};

string name = "Dakota";
Console.WriteLine(convert(name));
}
}

您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func<T, TResult>委托。(有关 lambda 表达式的简介,请参见 Lambda 表达式和 Lambda 表达式(C# 编程指南)。)

using System;

public class Lambdaexpression_r_r_r
{
public static void Main()
{
Func<string, string> convert = s => s.ToUpper();

string name = "Dakota";
Console.WriteLine(convert(name));
}
}

Lambda 表达式的基础类型是泛型 Func 委托之一。这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。尤其是,因为 System.Linq 命名空间中许多类型方法具有 Func<T, TResult> 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func<T, TResult> 委托。

示例
下面的示例演示如何声明和使用 Func<T, TResult>委托。此示例声明一个 Func<T, TResult>变量,并为其分配了一个将字符串中的字符转换为大写的 lambda 表达式。随后将封装此方法的委托传递给 Select 方法,以将字符串数组中的字符串更改为大写。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

static class Func
{
static void Main(string[] args)
{
// Declare a Func variable and assign a lambda expression_r_r_r to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper();

// Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector);

// Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);
}
}


泛型中协变和逆变的定义:

Animal animal = new Dog(); //类型的隐式转换
Dog dog = (Dog)animal; //类型的强制转换


IMyInterface<Animal> iAnimal = null;
IMyInterface<Dog> iDog = null;
iAnimal = iDog;// “子类”向“父类”转换,即泛型接口的协变
iDog = iAnimal;// “父类”向“子类”转换,即泛型接口的逆变



泛型类型的约束有:
派生约束:派生约束要求类型实参与指定

的类型相关联
列子:ZClass<T> where T:YClass<XClass> 或者 XClass<T1,T2> where T1:T2

接口约束:类型约束也可以要求类型实参实现一个接口。

值类型约束:将一个类型形参限制为值类型,值类型的约束使用 struct关键字。

引用类型
约束:将一个类型形参限定为引用类型。(类,接口,委托,字符串,数组)都称为引用类型,引用类型的约束使用class.

构造函数约束:构造函数的使用关键字为NEW。(new 关键字有三种用法 分别没 运算符 修饰符 定义约束)


除了值类型,如下类型不能用作约束

seale 密封类 开放式构造函数 基元类型 system.Array
system.Delegate

system.Enum

system.valueType




相关主题
相关文档
最新文档