超载 +运算符将两个数组相加

发布于 2024-08-10 01:10:24 字数 731 浏览 6 评论 0原文

这段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫七 2024-08-17 01:10:24

运算符必须在“相关”类的主体内声明。例如:

public class Foo
{
    int X;

    // Legal
    public static int operator+(int x, Foo y);

    // This is not
    public static int operator+(int x, int y);
}

由于您无权访问数组的实现,因此最好的选择是将数组包装在您自己的实现中,以便您可以提供其他操作(这是使运算符+工作的唯一方法

。另一方面,您可以定义一个扩展方法,例如:

public static class ArrayHelper
{
    public static int[] Add(this int[] x, int[] y) { ... }
}

仍然会导致自然调用 (x.Add(y)),同时避免将数组包装在您自己的类中。

Operators must be declared inside a "related" class' body. For instance:

public class Foo
{
    int X;

    // Legal
    public static int operator+(int x, Foo y);

    // This is not
    public static int operator+(int x, int y);
}

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:

public static class ArrayHelper
{
    public static int[] Add(this int[] x, int[] y) { ... }
}

The will still lead to natural calls (x.Add(y)) while avoiding to wrap arrays in your own class.

回首观望 2024-08-17 01:10:24

您可以使用如下内容:

class Program {
  static void Main(string[] args) {
    const int n = 5;

    var a = new int[n] { 1, 2, 3, 4, 5 }.WithOperators();
    var b = new int[n] { 5, 4, 3, 2, 1 };

    int[] c = a + b;

    for (int i = 0; i < c.Length; i++) {
      Console.Write("{0} ", c[i]);
    }

    Console.WriteLine();
  }
}

public static class Int32ArrayExtensions {
  public static Int32ArrayWithOperators WithOperators(this int[] self) {
    return self;
  }
}

public class Int32ArrayWithOperators {
  int[] _array;

  public Int32ArrayWithOperators(int[] array) {
    if (array == null) throw new ArgumentNullException("array");
    _array = array; 
  }

  public static implicit operator Int32ArrayWithOperators(int[] array) {
    return new Int32ArrayWithOperators(array); 
  }
  public static implicit operator int[](Int32ArrayWithOperators wrapper) {
    return wrapper._array;
  }

  public static Int32ArrayWithOperators operator +(Int32ArrayWithOperators left, Int32ArrayWithOperators right) {
    var x = left._array;
    var y = right._array;
    return x.Zip(y, (a, b) => a + b).ToArray();
  }
}

基于相关的帖子我写的。

You can use something like this:

class Program {
  static void Main(string[] args) {
    const int n = 5;

    var a = new int[n] { 1, 2, 3, 4, 5 }.WithOperators();
    var b = new int[n] { 5, 4, 3, 2, 1 };

    int[] c = a + b;

    for (int i = 0; i < c.Length; i++) {
      Console.Write("{0} ", c[i]);
    }

    Console.WriteLine();
  }
}

public static class Int32ArrayExtensions {
  public static Int32ArrayWithOperators WithOperators(this int[] self) {
    return self;
  }
}

public class Int32ArrayWithOperators {
  int[] _array;

  public Int32ArrayWithOperators(int[] array) {
    if (array == null) throw new ArgumentNullException("array");
    _array = array; 
  }

  public static implicit operator Int32ArrayWithOperators(int[] array) {
    return new Int32ArrayWithOperators(array); 
  }
  public static implicit operator int[](Int32ArrayWithOperators wrapper) {
    return wrapper._array;
  }

  public static Int32ArrayWithOperators operator +(Int32ArrayWithOperators left, Int32ArrayWithOperators right) {
    var x = left._array;
    var y = right._array;
    return x.Zip(y, (a, b) => a + b).ToArray();
  }
}

Based on a related post that I wrote.

原谅过去的我 2024-08-17 01:10:24

它规定运算符的参数之一必须与运算符函数所属的类型相同。因此,如果运算符函数是 MyClass 的成员,则参数的类型需要为 MyClass 类型。

class MyClass
{
 ...

public static int[] operator+(MyClass 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);
  }
}

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.

class MyClass
{
 ...

public static int[] operator+(MyClass 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);
  }
}
万劫不复 2024-08-17 01:10:24

当您想要在类型 AA 和 BB 之间重载 le + 运算符时,您必须在类 AA 或 BB 中执行此操作,而不是在名为 Program 的类中执行(就像您所做的那样)。

不幸的是,您无法在 Array 类中编写代码

您可以做的是

  • 创建自己的实现 IList 的类
  • ,并将 + 运算符放在该类上。

如果您需要更详细的实施,请问我。

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

  • create your own class that implements IList
  • and put the + operator on that class.

If you need more detailed implementation, just ask me.

怪我太投入 2024-08-17 01:10:24

您只能将运算符添加到您自己创建的类型中。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文