Login

C# Complex Math Source Code

A C# Class to Perform Arithmetic on Complex Numbers

Although the C# language has been used quite successfully in many kinds of projects, it still remains to be proven proficient for scientific computing. Questions remain as to whether or not C# can measure up to the likes of FORTRAN and C++ for scientific and mathematical projects.

Arithmetic and algebra on complex numbers is important in many fields of science and engineering. Even though C# does not support an intrinsic complex number data type, it doesn't stop you from creating your own. You can do so using a struct, which has value semantics. The purpose of this article is to present my attempt to fashion a complex arithmetic class for C#.

Using the Code

The struct Complex is completely contained within the namespace ComplexMath in the file Complex.cs. There are three separate program files included in the download as well as a documented help file for the project. You can run the program either by using the ComplexMath.dll reference, or by using the ComplexMath library code directly by simply adding the Complex.cs file to a project. You must, in either case, add the header using ComplexMath; to the main project file. If you decide to go the DLL route, first build the library (DLL) by running the CompexMath.sln file from which you can generate two ComplexMath.dll files, one each in bin/Release and bin/Debug.

Steps to Use the Library as a DLL

  • Start a new C# console application.
  • Build both the Debug and Release versions.
  • Place the Debug and Release versions of the ComplexMath.dll in their respective bin folders.
  • From Project/Add Reference menus, find the ComplexMath.dll for the version (debug or release) and add it to the project.
  • Add the header: using ComplexMath; to your program file.
  • Use the accompanying TestComplexMath.cs file as a prototype for your own program.


  • The original version of this source code (November 2007) can be obtained from The Code Project

    Download Current Version (9/30/2010): C# Complex Math Source and Demo Project