C#中泛型扩展方法的疑问
首先有以下两个类publicclassSwapper<T>{publicstaticvoidSwap(refTx,refTy){Ttmp=x;x=y;y=tmp;}}pu...
首先有以下两个类
public class Swapper<T>
{
public static void Swap(ref T x, ref T y)
{
T tmp = x;
x = y;
y = tmp;
}
}
public static class SwapperExtension
{
public static void Swap<T>(this Swapper<T> sw, ref T x1, ref T x2, ref T x3)
{
T tmp = x1;
x1 = x2;
x2 = x3;
x3 = tmp;
}
public static void Swap(this Swapper<int> sw, ref int[] x)
{
if (x.Length > 1)
{
int tmp = x[0];
for (int i = 0; i < x.Length - 1; i++)
{
x[i]=x[i+1];
}
x[x.Length - 1] = tmp;
}
}
}
第二个类用于泛型的·扩展方法。在主程序中,为什么
Swapper<int> sw1 = new Swapper<int>();
int x1 = 5, x2 = 10;
sw1.Swap(ref x1, ref x2);
是错误的? 展开
public class Swapper<T>
{
public static void Swap(ref T x, ref T y)
{
T tmp = x;
x = y;
y = tmp;
}
}
public static class SwapperExtension
{
public static void Swap<T>(this Swapper<T> sw, ref T x1, ref T x2, ref T x3)
{
T tmp = x1;
x1 = x2;
x2 = x3;
x3 = tmp;
}
public static void Swap(this Swapper<int> sw, ref int[] x)
{
if (x.Length > 1)
{
int tmp = x[0];
for (int i = 0; i < x.Length - 1; i++)
{
x[i]=x[i+1];
}
x[x.Length - 1] = tmp;
}
}
}
第二个类用于泛型的·扩展方法。在主程序中,为什么
Swapper<int> sw1 = new Swapper<int>();
int x1 = 5, x2 = 10;
sw1.Swap(ref x1, ref x2);
是错误的? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询