什么是允许复数和虚数的C#数据类型?

jon*_*hie 1 c# complex-numbers

我正在创建一个C#项目,它接受两个复数的用户输入并完成对它们的数学运算.我遇到的问题是如何解析作为-1的平方根.

double operandA = 0;
double operandB = 0;
double result = 0;
string textA, textB;
string error = "The value you entered is invalid, try again";

private void plus_Btn_Click(object sender, EventArgs e)
{
    result = operandA + operandB;
}

private void Subtract_btn_Click(object sender, EventArgs e)
{
    result = operandA - operandB;
}

private void mult_Btn_Click(object sender, EventArgs e)
{
    result = operandA * operandB;
}

private void divide_Btn_Click(object sender, EventArgs e)
{
    result = operandA / operandB;
}

private void textBox2_Leave(object sender, EventArgs e)
{
    textB = textBox2.Text;
    if (double.TryParse(textB, out operandB))
        operandB = double.Parse(textB);
    else
    {
        MessageBox.Show(error);
        textBox2.Select();
    }
}

private void textBox1_Leave(object sender, EventArgs e)
{
    textA = textBox1.Text;
    if (double.TryParse(textA, out operandA))
        operandA = double.Parse(textA);
    else
    {
        MessageBox.Show(error);
        textBox1.Select();
    }
}
Run Code Online (Sandbox Code Playgroud)

它适用于常规数字,小数和负数,但我无法弄清楚如何做我需要的i的值.有人可以帮忙吗?有人建议我使用System.Numeric.Complex,但每当我尝试使用"Using System.Numerics.Complex"或简单地"使用System.Numerics"时,它会说'System'中不存在这种类型/命名空间".

Tit*_*mir 7

如果要支持复数操作,则应考虑使用 System.Numerics.Complex

Complex c = Complex.Sqrt(-1);
Console.WriteLine(c + 1);
Run Code Online (Sandbox Code Playgroud)

有关此类型的文档,请参见此处