微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/csharp/100348.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐