二进制运算符的参数之一必须是包含类型 [英] One of the parameters of a binary operator must be the containing type

查看:186
本文介绍了二进制运算符的参数之一必须是包含类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下其中一个类中编写了以下代码

I have the following code written in one of the class

class LD_L_Entry
{
public static List<LD_L_Entry> operator + (List<LD_L_Entry> A,List<LD_L_Entry> B)
{
   /*code*/
}

}



当我构建项目时,出现以下错误
二进制运算符的参数之一必须是包含类型

Google对其进行了搜索,但找不到正确的解决方案.



when i build the project I get the following error
One of the parameters of a binary operator must be the containing type

Googled it but was not able to find the correct solution for it

推荐答案

此代码看起来不错.我单独构建它,没有收到任何错误.
尝试清理并再次构建解决方案.
This code looks ok. I built it separately and did not get any error.
Try doing a clean and the build the solution again.


请参阅此处 C#运算符重载 [ ^ ]


您的类显然与运算符中的两个参数都不相同.根据设计,您不能扩展或覆盖.NET Framework系统定义类型的运算符.您的情况是列表".

我不介意,但其他人会认真地做 [ ^ ]. C#在平台开发中经常使用,其参数与FastCGI环境中的脚本语言的参数不同.

无论如何,请尝试:


Your class is clearly NOT the same type as either of the parameters in the operator. By design you cannot extend or override operators for the System defined Types of the .NET Framework. In your case ''List''.

I wouldn''t mind but others seriously do[^]. C# is used a lot in platform development and arguments differ from those of a scripting language in a FastCGI environment.

Anyways, try:


void Main()
{
    var x = new LD_L<string>(){"a"};
    var y = new LD_L<string>(){"b"};
    var z = x+y;
}
class LD_L<LD_L_Entry> : List<LD_L_Entry>
{
    public static LD_L<LD_L_Entry> operator + (LD_L<LD_L_Entry> A,LD_L<LD_L_Entry> B)
    {
        /*code*/
        Console.WriteLine("foo and bar were pluzz-oped: " + A.GetType().ToString() + "+" + B.GetType().ToString());
        return null;
    }
}



结果:
foo和bar令人毛骨悚然:UserQuery + LD_L`1 [System.String] + UserQuery + LD_L`1 [System.String]

注意:
您不需要.GetType().ToString()、. ToString()就足够了.



Result:
foo and bar were pluzz-oped: UserQuery+LD_L`1[System.String]+UserQuery+LD_L`1[System.String]

Note:
You do not need .GetType().ToString(), .ToString() would suffice.


这篇关于二进制运算符的参数之一必须是包含类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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