1.Int 32位长度20亿左右
2.GetType()方法,Object自带的方法,获取类型
3.泛型解决的问题:多方法,多类直接采用Object频繁装箱拆箱操作的资源浪费。(泛型是个语法糖)
4.代码:
namespace Generic
{
class Program
{
static void Main(string[] args)
{
People p = new People() //封装了一个类
{ //初始化器对类的属性进行快速初始化
id = 1,
name = "www"
};
Console.WriteLine("***********************************");
Generic.showT<People>(p); // 泛型方法调用,传入参数类型People,则参数就必须是P、
Generic.showT<int>(1); //参数int类型,则必须是int参数
Console.WriteLine("************************************"); //多参数个数,调用
Generic.ShowDT<int, string, Double, float>(1, "1", 12.00, 124, 1);
Console.WriteLine("**********************************");//省略类型参数的泛型调用
Generic.showT(p);//直接传入参数,没有指定参数类型,泛型可以识推断参数类型
Console.ReadKey();
}
}
public class Generic
{
/// <summary>
/// 创建Generic类,内部有个泛型方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="P"></param>
public static void showT<T>(T Parameter) //创建泛型方法
{
Console.WriteLine(Parameter.GetType());
}
public static void ShowDT<T, S, W, G>(T TParemeter, S SParemeTer, W WParemeter, G GParemeter, int i)
{
Console.WriteLine("多个参数类型的参数");
Console.WriteLine(string.Format("T的类型是{0},S的类型是{1},W的类型是{2},G的类型是{3},I的类型是{4}", TParemeter.GetType(), SParemeTer.GetType(), WParemeter.GetType(), GParemeter.GetType(), i.GetType()));
}
}
public class People
{
public void Show()
{
Console.WriteLine("People");
Console.WriteLine("用户id{0},名称{1}", id, name);
}
public int id { get; set; }
public string name { get; set; }
}
}
5.泛型的特性
1).传入不同类型的参数泛型方法调用
2).同一个泛型方法,可以使用多个泛型的参数类型,而且泛型类型的个数,和参数的个数是可以不同的的
3).泛型可以不传入类型参数限制,泛型会自动辨认参数类型