先写一个函数:

 

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

 

看来范型还挺智能的,呵呵

 

posted on 2007-06-13 13:50  今夜太冷  阅读(336)  评论(0)    收藏  举报