c# – 实现与通用方法参数的接口VS实现方法参数的接口

我对编程课程中的内容感到很困惑.

简而言之 – 我发现了一种具有这种结构的方法

public void MethodA<T>(T a) where T : IComparable
    {
        //...
    }

据我所知 – 我们可以使用相同的确切效果

public void MethodB(IComparable a)
    {
       //... 
    }

这两种方式在某种程度上是否有所不同,并且其中一种方式优于另一种方式?如果是这样,那么如何才能看出其中一个更好用?

非常感谢!

解决方法

我很好奇,所以我自己做了一个小测试代码
public interface ITest
{
    int Value { get; set; }
}
public struct TestStruct : ITest
{
    public int Value { get; set; }
}

private static void TestMethodGeneric<T>(T value) where T : ITest
{
}
private static void TestMethodNonGeneric(ITest value)
{
}

在我的主要方法中,我使用两个调用

TestStruct ts = new TestStruct {Value = 10};
TestMethodNonGeneric(ts);
TestMethodGeneric(ts);

这是由此产生的IL代码

非通用:

通用:

所以你看,在通用版本中,使用了特定的类型,因此不会发生装箱.
在非泛型版本中,struct值必须被转换为ITest,因此它被装箱.因此,通用版本具有(非常小的)性能优势.

仅仅是我的两分钱,这两种方法可能存在其他或多或少的重要差异.

IL_0031: ldloc.0 // ts IL_0032: Box Tests.Program/*02000002*//TestStruct/*02000026*/ IL_0037: call void Tests.Program/*02000002*/::TestMethodNonGeneric(class Tests.Program/*02000002*//ITest/*02000025*/)/*
public interface ITest
{
    int Value { get; set; }
}
public struct TestStruct : ITest
{
    public int Value { get; set; }
}

private static void TestMethodGeneric<T>(T value) where T : ITest
{
}
private static void TestMethodNonGeneric(ITest value)
{
}
public interface ITest { int Value { get; set; } } public struct TestStruct : ITest { public int Value { get; set; } } private static void TestMethodGeneric<T>(T value) where T : ITest { } private static void TestMethodNonGeneric(ITest value) { }002*/
IL_003c: nop
IL_0059: ldloc.0 // ts IL_005a: call void Tests.Program/*02000002*/::TestMethodGeneric<valuetype Tests.Program/*02000002*//TestStruct/*02000026*/>(!!0/*valuetype Tests.Program*//*02000002*//*/TestStruct*//*02000026*//**/)/*
public interface ITest
{
    int Value { get; set; }
}
public struct TestStruct : ITest
{
    public int Value { get; set; }
}

private static void TestMethodGeneric<T>(T value) where T : ITest
{
}
private static void TestMethodNonGeneric(ITest value)
{
}
public interface ITest { int Value { get; set; } } public struct TestStruct : ITest { public int Value { get; set; } } private static void TestMethodGeneric<T>(T value) where T : ITest { } private static void TestMethodNonGeneric(ITest value) { }001*/
IL_005f: nop

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...