# Gewusst wie: Verwenden der Operatorüberladung zum Erstellen einer Klasse für komplexe Zahlen (C#-Programmierhandbuch)How to: Use Operator Overloading to Create a Complex Number Class (C# Programming Guide)

In diesem Beispiel wird gezeigt, wie Sie die Operatorüberladung verwenden können, um die komplexe Zahlenklasse `Complex` zu erstellen, die die komplexe Addition definiert.This example shows how you can use operator overloading to create a complex number class `Complex` that defines complex addition. Das Programm zeigt den Imaginär- und den Realteil der Zahlen und das Ergebnis der Addition durch Überschreiben der `ToString`-Methode an.The program displays the imaginary and the real parts of the numbers and the addition result using an override of the `ToString` method.

## BeispielExample

``````public struct Complex
{
public int real;
public int imaginary;

// Constructor.
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

// Specify which operator to overload (+),
// the types that can be added (two Complex objects),
// and the return type (Complex).
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

// Override the ToString() method to display a complex number
public override string ToString()
{
return (System.String.Format("{0} + {1}i", real, imaginary));
}
}

class TestComplex
{
static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);

Complex sum = num1 + num2;

// Print the numbers and the sum by using the overridden
// ToString method.
System.Console.WriteLine("First complex number:  {0}", num1);
System.Console.WriteLine("Second complex number: {0}", num2);
System.Console.WriteLine("The sum of the two numbers: {0}", sum);

// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");