为什么重载方法在文献只有符合CLS不同 [英] Why is overloaded method differing in ref only CLS compliant
问题描述
公共语言规范是在方法重载相当严格。
被允许
方法的基础上的数量和类型也可以只载其参数和泛型方法,它们的泛型参数的数量。
块引用>
为什么这个代码符合CLS(无CS3006警告的情况下?)根据CSC
使用系统;
[大会:CLSCompliant(真)]
公共类的测试{
公共静态无效期待< T>(T ARG)
{
}
公共静态无效期待< T>(REF牛逼ARG)
{
}
公共静态无效的主要()
{
}
}
解决方案这是符合CLS,因为类型不同。超载的规则要求的标准之一(或更多)同时得到满足,不是所有的人。
A
REF ŧ
(或出牛逼
,这是使用同类型不同的语义相同)正在申报参考一T
引用(类)或实例(在值类型的情况下)。
有关详细信息,查找的
Type.MakeByRefType()
方法 - 它创建代表原始类型,如引用类型为T
这个返回T&放大器;
(在C ++符号)Common Language Specification is quite strict on method overloads.
Methods are allowed to be overloaded only based on the number and types of their parameters, and in the case of generic methods, the number of their generic parameters.
Why is this code CLS compliant (no CS3006 warning) according to csc?
using System; [assembly: CLSCompliant (true)] public class Test { public static void Expect<T>(T arg) { } public static void Expect<T>(ref T arg) { } public static void Main () { } }
解决方案This is CLS-compliant because the types differ. The rules for overloading are requiring one (or more) of the criteria to be met, not all of them at the same time.
A
ref T
(orout T
, which is using the same with same type different semantics) is declaring a "reference" to aT
reference (for classes) or the instance (in case of value types).For more details, look up the
Type.MakeByRefType()
method - it creates the type representing a reference to the original type, e.g. for aT
this returns aT&
(in C++ notation).这篇关于为什么重载方法在文献只有符合CLS不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!