Sqrt() Examples

Returns the square root of a given positive double.

Returns NaN value if used with negative number. NaN is a field in the Double class.

NaN stands for “Not a Number” and in this case, the square root of a negative number does not exist. It is imaginary, or not a number.

You can read more about NaN in the Microsoft documentation. And the square root method here.

General Syntax

Math.Sqrt(doubleNumber);

Example

1
2
Console.WriteLine(Math.Sqrt(9));
Console.WriteLine(Math.Sqrt(-9));

Console Output

3
NaN