C# Math Sqrt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.Sqrt(d) returns the square root of a number: the number that gives d when multiplied by itself. Math.Sqrt(16) is 4, because 4 x 4 = 16.

Think of a square garden with an area of 144 square metres. Each side is Math.Sqrt(144) = 12 metres long. Square roots appear in distances, standard deviation, and many formulas. The method takes a double and returns a double. It is the same as Pow(d, 0.5), only simpler to read and a bit faster.

This is a static member of the static class System.Math. You call it with the class name, such as Math.Sqrt(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Sqrt is in every .NET version. It takes and returns double.

Syntax

Pass one number. An int is converted to double for you, and the result is a double.

C#
double a = Math.Sqrt(16);     // 4
double b = Math.Sqrt(144);    // 12
double c = Math.Sqrt(-1);     // NaN (no real square root)
CallWhat it does
Math.Sqrt(16)Returns 4.
Math.Sqrt(0)Returns 0.
Math.Sqrt(-1)Returns NaN. A negative number has no real square root.
Math.Sqrt(double.PositiveInfinity)Returns Infinity.

Sqrt never throws for a bad input. It returns NaN for a negative number, so check double.IsNaN(result) or test d < 0 first. Most results are not whole numbers and are not stored exactly, so round them (Math.Round(Math.Sqrt(2), 4)) before you print or compare. There is no decimal overload.

Library declarations
C#
public static double Sqrt(double d);

Parameters

ParameterDescription
dRequired. The number to take the square root of. It should be zero or positive.

Return Value

Return ValueDescription
doubleThe positive square root of d. NaN if d is negative or NaN. Infinity if d is positive infinity.

C# Examples

Example 1 — Square Roots of Perfect Squares

Take the square root of numbers whose answers are whole.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.Sqrt(16));
        Console.WriteLine(Math.Sqrt(144));
        Console.WriteLine(Math.Sqrt(1));
        Console.WriteLine(Math.Sqrt(0));
        Console.WriteLine(Math.Sqrt(10000));
    }
}

How It Works

  1. 16 gives 4. The square root asks "which number times itself makes 16?". The answer is 4.
  2. 144 gives 12. Perfect squares such as 144 have a whole-number root, and the exact value prints without a decimal point.
  3. 1 and 0. The square root of 1 is 1, and the square root of 0 is 0. Both are exact.
  4. Big numbers. Sqrt(10000) is 100. The argument is an int, which C# converts to double automatically.

Example 2 — Distance Between Two Points

Use the Pythagorean theorem with Sqrt, then check whether an integer is a perfect square.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        double dx = 4 - 1;
        double dy = 6 - 2;
        double distance = Math.Sqrt(dx * dx + dy * dy);
        Console.WriteLine(distance);
        Console.WriteLine(Math.Round(Math.Sqrt(2), 4).ToString(CultureInfo.InvariantCulture));

        int a = 49;
        int b = 50;
        Console.WriteLine((int)Math.Sqrt(a) * (int)Math.Sqrt(a) == a);
        Console.WriteLine((int)Math.Sqrt(b) * (int)Math.Sqrt(b) == b);
    }
}

How It Works

  1. The formula. From point (1, 2) to point (4, 6), the horizontal gap is 3 and the vertical gap is 4. The distance is the square root of 3 x 3 + 4 x 4.
  2. A 3-4-5 triangle. 9 + 16 is 25, and Math.Sqrt(25) is exactly 5.
  3. Not a whole root. The square root of 2 goes on forever (1.41421356...). Rounding to 4 decimals gives 1.4142.
  4. Perfect-square test. Take the root, cut it to an int, and square it again. 49 gives 7 x 7 = 49, so True. For 50, 7 x 7 is 49, so False.

Example 3 — Negative Input Gives NaN

See that Sqrt(-1) does not throw, how to detect NaN, and how to write a safe helper.

C#
using System;

class Program {
    static double SafeSqrt(double value) {
        return value < 0 ? 0 : Math.Sqrt(value);
    }

    static void Main() {
        double bad = Math.Sqrt(-1);
        Console.WriteLine(bad);
        Console.WriteLine(double.IsNaN(bad));

        Console.WriteLine(SafeSqrt(-9));
        Console.WriteLine(SafeSqrt(9));
        Console.WriteLine(Math.Sqrt(double.PositiveInfinity));
    }
}

How It Works

  1. No real root. No real number multiplied by itself gives -1. Math.Sqrt(-1) returns NaN instead of throwing an exception.
  2. Detect it. double.IsNaN(bad) is True. Never use bad == double.NaN, because NaN is not equal to anything.
  3. A safe helper. SafeSqrt returns 0 for negatives and the real root otherwise. Choose the fallback that fits your program: 0, an error, or NaN.
  4. Infinity. The square root of positive infinity is still Infinity.

Frequently Asked Questions

It returns the square root of a number as a double. Math.Sqrt(16) is 4 and Math.Sqrt(144) is 12.
A negative number has no real square root. C# follows the IEEE floating-point rules and returns NaN (not a number) instead of throwing an exception. Check double.IsNaN(result) to detect it.
Take r = (int)Math.Sqrt(n) and test r * r == n. For very large numbers, use long and double-check neighbours, because a double cannot hold every large integer exactly.
They give the same result for normal numbers. Sqrt is simpler to read and usually a little faster. A negative number gives NaN in both.
No. Math is a static class, so you call Math.Sqrt(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Tan()

Return the tangent of an angle given in radians.

Tan() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful