超载 +运算符将两个数组相加
这段 C# 代码有什么问题?我尝试重载 + 运算符来添加两个数组,但收到如下错误消息:
二元运算符的参数之一必须是包含类型。
class Program
{
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 1, 2, 3, 4, 5 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] c = new int[n];
// c = Add(a, b);
c = a + b;
for (int i = 0; i < c.Length; i++)
{
Console.Write("{0} ", c[i]);
}
Console.WriteLine();
}
public static int[] operator+(int[] x, int[] y)
// public static int[] Add(int[] x, int[] y)
{
int[] z = new int[x.Length];
for (int i = 0; i < x.Length; i++)
{
z[i] = x[i] + y[i];
}
return (z);
}
}
What's wrong with this C# code? I tried to overload the + operator to add two arrays, but got an error message as follows:
One of the parameters of a binary operator must be the containing type.
class Program
{
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 1, 2, 3, 4, 5 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] c = new int[n];
// c = Add(a, b);
c = a + b;
for (int i = 0; i < c.Length; i++)
{
Console.Write("{0} ", c[i]);
}
Console.WriteLine();
}
public static int[] operator+(int[] x, int[] y)
// public static int[] Add(int[] x, int[] y)
{
int[] z = new int[x.Length];
for (int i = 0; i < x.Length; i++)
{
z[i] = x[i] + y[i];
}
return (z);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
运算符必须在“相关”类的主体内声明。例如:
由于您无权访问数组的实现,因此最好的选择是将数组包装在您自己的实现中,以便您可以提供其他操作(这是使运算符+工作的唯一方法
。另一方面,您可以定义一个扩展方法,例如:
仍然会导致自然调用 (
x.Add(y)
),同时避免将数组包装在您自己的类中。Operators must be declared inside a "related" class' body. For instance:
Since you don't have access to the implementation of arrays, your best bet would be to either wrap your arrays in your own implementation so you can provide additional operations (and this is the only way to make the operator+ work.
On the other hand, you could define an extension method like:
The will still lead to natural calls (
x.Add(y)
) while avoiding to wrap arrays in your own class.您可以使用如下内容:
基于相关的帖子我写的。
You can use something like this:
Based on a related post that I wrote.
它规定运算符的参数之一必须与运算符函数所属的类型相同。因此,如果运算符函数是 MyClass 的成员,则参数的类型需要为 MyClass 类型。
Its states that one of the parameters to the operator needs to be of the same type as the operator function is a member of. So if the operator function is a member of MyClass on of the parameters needs to be of type MyClass.
当您想要在类型 AA 和 BB 之间重载 le + 运算符时,您必须在类 AA 或 BB 中执行此操作,而不是在名为 Program 的类中执行(就像您所做的那样)。
不幸的是,您无法在 Array 类中编写代码。
您可以做的是
如果您需要更详细的实施,请问我。
When you want to overload le + operator between type AA and BB, you must do it in the class AA or BB and not in a class named Program (like you did).
Unfortunatly, you cannot write code in the Array class.
What you can do is to
If you need more detailed implementation, just ask me.
您只能将运算符添加到您自己创建的类型中。
int[]
是内置类型,您无法向其添加运算符。您可以创建自己的类来封装数组,并向其中添加运算符。
You can only add operators to a type that you create yourself. A
int[]
is a built in type you can't add operators to it.You could create your own class that encapsulates an array, and add the operator to it.