Math Class

Use the Math class to perform mathematical calculations.


We occasionally need to perform some mathematical calculations inside our programs. We may even want to create our own calculator. .NET provides us with the necessary tools out of the box.

The System namespace contains a static class called Math with methods for performing various mathematical operations.

For instance, we could obtain the square root of a number by simply calling the Math.Sqrt() method.

Note: he Math class is static, so all its members are also static. We use the class’ methods without instantiating it.

The Math class includes many methods, so it’s impossible to cover them all. We’ll see some of them in our examples, though. Here are some of the most popular:

  • Math.Pow(): This raises a number to a given power.
  • Math.Sqrt(): This finds the square root of a number.
  • Math.Cos(): This calculates the cosine of a given angle.
  • Math.Abs(): This returns the absolute value of a number.
  • Math.Cbrt(): This calculates the cubic root of a number.


Let’s create some useful programs as examples.

Calculate the area of a circle

Here’s the formula:

A=πr2A = \pi r^2

Our method accepts radius as a parameter. We take its square and multiply the result by π\pi. Fortunately, the Math class has a constant called PI that holds the value of π\pi:

Get hands-on with 1200+ tech skills courses.