加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:01:25 所属栏目:百科 来源:网络整理
导读:我对编程课程中的内容感到很困惑. 简而言之 – 我发现了一种具有这种结构的方法: public void MethodAT(T a) where T : IComparable { //... } 据我所知 – 我们可以使用相同的确切效果: public void MethodB(IComparable a) { //... } 这两种方式在某种程
我对编程课程中的内容感到很困惑.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读