您的位置:首页 > 教育 > 培训 > C# 泛型函数

C# 泛型函数

2024/10/31 9:51:09 来源:https://blog.csdn.net/weixin_45875105/article/details/139279837  浏览:    关键词:C# 泛型函数

在这里插入图片描述

1.非约束

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyGeneirc
{public class GeneircMethod{/// <summary>/// 泛型方法解决,一个方法,满足不同参数类型,做相同的事情/// </summary>/// <typeparam name="T">类型名称</typeparam>/// <param name="tParam">参数对象</param>public static void Show<T>(T tParam){Console.WriteLine("当前类:{0},参数类型名称:{1}, 值:{2}",typeof(GeneircMethod), tParam.GetType().Name, tParam.ToString());}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyGeneirc
{internal class Program{static void Main(string[] args){try{// 变量定义int iValue = 123;string strValue = "456";DateTime dtValue = DateTime.Now;object oValue = "Mr Sorry";// 泛型方法调用 Console.WriteLine("****************Geneirc***********************");// 泛型方法解决,一个方法,满足不同参数类型,做相同的事情GeneircMethod.Show<int>(iValue);GeneircMethod.Show<string>(strValue);GeneircMethod.Show<DateTime>(dtValue);GeneircMethod.Show<object>(oValue);// 泛型类GenericClass<int> genericInt = new GenericClass<int> { _t = 19 };// 约束Console.WriteLine("****************约束非约束***********************");People people = new People() { Id = 123, Name = "张三" };Chinese chinese = new Chinese() { Id = 234, Name = "李四" };Hubei hubei = new Hubei() { Id = 456, Name = "王五" };Japanese japanese = new Japanese() { Id = 678, Name = "田中" };// 无意义显示GeneircMethod.Show<People>(people);GeneircMethod.Show<Chinese>(chinese);GeneircMethod.Show<Hubei>(hubei);GeneircMethod.Show<Japanese>(japanese); }catch(Exception ex) {Console.WriteLine(ex.ToString());}Console.ReadLine();}}
}

在这里插入图片描述

2.泛型约束

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyGeneirc
{public class Constraint{/// <summary>/// 泛型约束/// 1.权利 可以使用基类的一切属性方法/// 2.义务 强制保证T 一定是People 或 其子类(不能是密封类,)/// </summary>public static void Show<T>(T tParam)where T : People, ISports // 基类约束{Console.WriteLine("当前类:{0},ID:{1}, Name:{2}",typeof(Constraint), tParam.Id, tParam.Name);tParam.Hi();}// 泛型约束相比父类 可以叠加,更加灵活public static void ShowBase(People tParam){Console.WriteLine("当前类:{0},ID:{1}, Name:{2}",typeof(Constraint), tParam.Id, tParam.Name);tParam.Hi();}public static T Get<T>(T t)where T : ISports // 接口约束{t.Pinpang();return t;}public static T GetClass<T>(T t)where T : class // 引用类型约束{T tNew = null; // T 必须是引用类型return t;}public static T GetStruct<T>(T t)where T : struct // 值类型约束{// 默认类型 根据T的不同 赋予默认值T tNew = default(T); // T 必须是值类型return t;}public static T GetNew<T>(T t)where T : new() // 无参数构造函数约束{// new 对象T tNew = new T(); // T 类必须有构造函数(无参数)return t;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace MyGeneirc
{internal class Program{static void Main(string[] args){try{People people = new People() { Id = 123, Name = "张三" };Chinese chinese = new Chinese() { Id = 234, Name = "李四" };Hubei hubei = new Hubei() { Id = 456, Name = "王五" };Japanese japanese = new Japanese() { Id = 678, Name = "田中" };Console.WriteLine("****************约束基类***********************");//Constraint.Show<People>(people);Constraint.Show<Chinese>(chinese);Constraint.Show<Hubei>(hubei);//Constraint.Show<Japanese>(japanese); // 编译报错Console.WriteLine("****************约束接口***********************");Constraint.Get<Chinese>(chinese);}catch(Exception ex) {Console.WriteLine(ex.ToString());}Console.ReadLine();}}
}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com