10
using System;    
using System.Math;    
class test

  {    
    public static void Main()    
      {    
        Console.Write("Enter any  value: ");    
        string s=Console.ReadLine();    
        double n = double.Parse(s);    
        double r = Math.sqrt(n);    
        Console.WriteLine(r);    
        Console.ReadKey();    
      }    
  }

I feel that every thing is clear in this code, but this code is giving compile errors:
A using namespace directive can only be applied to namespaces; 'System.Math' is a type not a namespace

How to use math functions? Where do we get a list of all math functions available in Math class?

Thank You.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Arjun
  • 1,049
  • 6
  • 23
  • 37

4 Answers4

30

Math is a static class, not a namespace. It is located in the System namespace.
Therefore, you only have to include the System namespace.
Simply use Math.Sqrt and drop the "using System.Math;" Note that it is Math.Sqrt and not Math.sqrt

Hope that helps ;-)

Warty
  • 7,237
  • 1
  • 31
  • 49
7

Starting with C# 6.0, you can use

using static System.Math;

if you don't want to write Math. all the time.

dashnick
  • 2,020
  • 19
  • 34
5

You've got a case sensitivity problem

double r = Math.Sqrt(n);

http://msdn.microsoft.com/en-us/library/system.math_members(VS.85).aspx

Jonathan
  • 5,953
  • 1
  • 26
  • 35
3

remove using System.Math;

You do need to reference Math class like above. using System; is enough

For reference and sample use, see Math Class

Asad
  • 21,468
  • 17
  • 69
  • 94