泛型入门初探

引语:

一直想写博客记录学习中遇到的知识小结,出于懒惰也一直没有付诸实践。故从现在开始坚持每天都写一些东西记录零散的小知识点,如有错误之处,烦请指正。

泛型的定义与调用:

具体代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleGeneric
{
class Program
{
static void Main(string[] args)
{
MyInfo("cxx");
MyInfo1(1);
ShowInfo(new People { Name="cxx",Age=25});
Console.ReadLine();
}
///


/// 引用类型约束
///

///
///
public static void MyInfo(T t) where T:class
{
Console.WriteLine("name:" + t.ToString());
}
///
/// 值类型约束
///

///
///
public static void MyInfo1(T t) where T : struct
{
Console.WriteLine("参数类型" + t.GetType());
}
public static void ShowInfo(T t) where T:People
{
Console.WriteLine("姓名:{0},年龄:{1}",t.Name,t.Age);
}
}
public class People
{
public string Name { get; set; }
public int Age { get; set; }
}
}



你可能感兴趣的:(泛型入门初探)