Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0327 Complex

The Complex struct is for representing complex numbers with real and imaginary components of type double. Complex resides in the System.Numerics.dll assembly. To use Complex, instantiate the struct, specifying the real and imaginary values: using System; using System.Numerics; class Sample { public static void Main() { Complex c1 = new Complex(2, 3.5); Complex c2 = new Complex(3, 0); Console.WriteLine(c1.Real); // 2 Console.WriteLine(c1.Imaginary); // 3.5 Console.WriteLine(c1.Phase); // 1.05165021254837 Console.WriteLine(c1.Magnitude); // 4.03112887414927 } } The output: 2 3.5 1.05165021254837 4.03112887414927 Construct a Complex number by specifying magnitude and phase using System; using System.Numerics; class Sample { public static void Main() { Complex c1 = Complex.FromPolarCoordinates(1.3, 5); Console.WriteLine(c1); } } The output: (0.368760841102194, -1.24660155706208)