先写一个函数:
public void WriteSth<T>(T x, T y)
{
if (typeof(T) == typeof(int))
{
Response.Write(x);
}
if( typeof( T ) == typeof( string ))
{
Response.Write( y );
}
}
然后调用这个函数:
调用1:
protected void Page_Load(object sender, EventArgs e)
{
WriteSth<int>(1, 2);
}
结果: 1
调用2:
protected void Page_Load(object sender, EventArgs e)
{
WriteSth<string>("1", "2");
}
结果:2
调用3:
protected void Page_Load(object sender, EventArgs e)
{
WriteSth(1, 2);
}
结果:
1
调用4:
protected void Page_Load(object sender, EventArgs e)
{
WriteSth("1", "2");
}
结果: 2
看来范型还挺智能的,呵呵
浙公网安备 33010602011771号