在开发过程中,我们定义的泛型类或泛型方法默认都是无约束的,可以传入任何类型的参数,在有的时候方法处理逻辑并不一定能够处理某种类型的参数,就会导致程序运行出错。这个时候就需要我们给泛型类或泛型方法指定某些约束,以便于程序安全稳定的运行。
参考代码:
将方法约束为引用类型参数:
public static string GetT1<T>(T t) where T : class
{
return typeof(T) + "|+|" + t;
}
将方法约束为值类型参数:
public static string GetT2<T>(T t) where T : struct
{
return typeof(T) + "|+|" + t;
}
将方法约束为必须带有无参构造函数的类型:
public static string GetT3<T>(T t) where T : new()
{
return typeof(T) + "|+|" + t;
}
将方法约束为带有公共无参构造函数的引用类型
public static string GetT4<T>(T t) where T : class, new()
{
return typeof(T) + "|+|" + t;
}
将方法约束为
/// 第一个参数:只能是带有公共无参构造函数的引用类型
/// 第二个参数:只能是值类型
public static string GetT5<T, S>(T t, S s)
where T : class, new()
where S : struct
{
return typeof(T) + "|+|" + t + "|+|" + typeof(S) + "|+|" + s;
}
在实际应用中不止上面展示的这几种约束,还有很多约束需要在我们使用时去应用。而且约束是既可以加在方法后面也可以加在类名后面的。
class GenericDemo1<T, S, U>
where T : class
where S : class
where U : struct
{
}
下面列出了官方文档提供的约束说明:
约束 | 描述 |
where T : struct | 类型参数必须是不可为 null 的值类型。 有关可为 null 的值类型的信息,请参阅可为 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 约束。 有关详细信息,请参阅default约束规范建议。 |
where T : unmanaged | 类型参数必须是不可为 null 的非托管类型。 unmanaged 约束表示 struct 约束,且不能与 struct 约束或 new() 约束结合使用。 |
where T : new() | 类型参数必须具有公共无参数构造函数。 与其他约束一起使用时,new() 约束必须最后指定。 new() 约束不能与 struct 和 unmanaged 约束结合使用。 |
where T : <base class name> | 类型参数必须是指定的基类或派生自指定的基类。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 必须是从指定基类派生的不可为 null 的引用类型。 |
where T : <base class name>? | 类型参数必须是指定的基类或派生自指定的基类。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 可以是从指定基类派生的可为 null 或不可为 null 的类型。 |
where T : <interface name> | 类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。 在 C# 8.0 及更高版本中的可为 null 上下文中,T 必须是实现指定接口的不可为 null 的类型。 |
where T : <interface name>? | 类型参数必须是指定的接口或实现指定的接口。 可指定多个接口约束。 约束接口也可以是泛型。 在 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 的引用类型。 |
完整演示代码:
class GenericDemo
{
public static void GenericMain()
{
Console.WriteLine(GetT1("1-1"));
Console.WriteLine(GetT2(1));
Console.WriteLine(GetT3(2.1));
Console.WriteLine(GetT4(new StringBuilder("ss")));
Console.WriteLine(GetT5(new StringBuilder("ss"), 6.666666));
}
/// <summary>
/// 将方法约束为引用类型参数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetT1<T>(T t) where T : class
{
return typeof(T) + "|+|" + t;
}
/// <summary>
/// 将方法约束为值类型参数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetT2<T>(T t) where T : struct
{
return typeof(T) + "|+|" + t;
}
/// <summary>
/// 将方法约束为必须带有无参构造函数的类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetT3<T>(T t) where T : new()
{
return typeof(T) + "|+|" + t;
}
/// <summary>
/// 将方法约束为带有公共无参构造函数的引用类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GetT4<T>(T t) where T : class, new()
{
return typeof(T) + "|+|" + t;
}
/// <summary>
/// 将方法约束为
/// 第一个参数:只能是带有公共无参构造函数的引用类型
/// 第二个参数:只能是值类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="S"></typeparam>
/// <param name="t"></param>
/// <param name="s"></param>
/// <returns></returns>
public static string GetT5<T, S>(T t, S s)
where T : class, new()
where S : struct
{
return typeof(T) + "|+|" + t + "|+|" + typeof(S) + "|+|" + s;
}
}
class GenericDemo1<T, S, U>
where T : class
where S : class
where U : struct
{
}
输出结果如下: