首页 > 代码库 > C# 泛型初探

C# 泛型初探

初探的类:

public class TClass
{
/// <summary>
/// int参数
/// </summary>
/// <param name="iParameter"></param>
public static void ShowInt(int iParameter)
{
Console.WriteLine("这里是TClass ShowInt{0},类型为{1}",iParameter,iParameter.GetType());
}

/// <summary>
/// long参数
/// </summary>
/// <param name="lParameter"></param>
public static void ShowLong(long lParameter)
{
Console.WriteLine("这里是TClass ShowLong{0},类型为{1}", lParameter, lParameter.GetType());
}

/// <summary>
/// object参数

///Object类型是任何类型的父类

///任何父类出现的地方,都可以用子类来代替
/// </summary>
/// <param name="lParameter"></param>
public static void Showobject(object tParameter)
{
Console.WriteLine("这里是TClass Showobject{0},类型为{1}", tParameter, tParameter.GetType());
}

/// <summary>
/// T 参数(泛型)
/// </summary>
/// <param name="lParameter"></param>
public static void ShowGeneric<T>(T Parameter)
{
Console.WriteLine("这里是TClass ShowGeneric{0},类型为{1}", Parameter, Parameter.GetType());
}
}

 

调用方法:

int iValue = http://www.mamicode.com/123;
long lValue = http://www.mamicode.com/456789;

Console.WriteLine("-----------------ShowInt,ShowLong--------------------");
TClass.ShowInt(iValue);
TClass.ShowLong(lValue);

Console.WriteLine("-----------------object参数--------------------");
TClass.Showobject(iValue);
TClass.Showobject(lValue);

Console.WriteLine("-----------------T 参数(泛型)--------------------");
TClass.ShowGeneric(iValue);
TClass.ShowGeneric(lValue);

结果:

123,System.Int32

456789,System.Int64

C# 泛型初探