具有相同签名的两种方法的互斥约束 [英] Mutally exclusive constraints on two methods with the same signature

查看:59
本文介绍了具有相同签名的两种方法的互斥约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这两种方法具有相同的签名,但约束不同

So these two methods have the same signature but different constraints

public static void Method<T>(ref T variable) where T : struct { }

public static void Method<T>(ref T variable) where T : class { }

但是它们不能在单个类中定义,因为它们具有相同的签名.但是在这种特殊情况下,它们是互斥的.(除非我对此有误)

But they cannot be defined in a single class because they have the same signatures. But in this particular case they're mutually exclusive. (Unless I'm wrong about that)

我知道您可以在 class struct 之外添加其他约束,但不能同时指定 struct class 的相同方法.那为什么编译失败呢?

I understand you can put additional constraints besides class and struct but you can't specify both struct and class on the same method. So why would this fail to compile?

推荐答案

@安东尼(链接).

就编译器而言,您有一个 duplicate 方法-相同数量和类型的参数.

As far as the compiler is concerned you have a duplicate method - same numbers and types of parameters.

这篇关于具有相同签名的两种方法的互斥约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆