为什么重载方法在文献只有符合CLS不同 [英] Why is overloaded method differing in ref only CLS compliant

查看:224
本文介绍了为什么重载方法在文献只有符合CLS不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共语言规范是在方法重载相当严格。



被允许

方法的基础上的数量和类型也可以只载其参数和泛型方法,它们的泛型参数的数量。




为什么这个代码符合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 (or out T, which is using the same with same type different semantics) is declaring a "reference" to a T 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 a T this returns a T& (in C++ notation).

这篇关于为什么重载方法在文献只有符合CLS不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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