C# System.Math 未识别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:21:37  来源:igfitidea点击:

System.Math un identified

c#mathematical-optimization

提问by Gokul

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

我觉得这段代码中的每一件事都很清楚,但是这段代码给出了编译错误:
using namespace 指令只能应用于命名空间;'System.Math' 是一种类型而不是命名空间

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

如何使用数学函数?我们从哪里获得 Math 类中所有可用数学函数的列表?

Thank You.

谢谢你。

采纳答案by Warty

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

Math 是一个静态类,而不是一个命名空间。它位于 System 命名空间中。
因此,您只需包含 System 命名空间。
只需使用 Math.Sqrt 并删除“using System.Math;” 请注意,它是 Math.Sqrt 而不是 Math.sqrt

Hope that helps ;-)

希望有帮助;-)

回答by Jonathan

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

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

回答by Asad

remove using System.Math;

消除 using System.Math;

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

您确实需要Math像上面那样引用类。using System;足够

For reference and sample use, see Math Class

有关参考和示例使用,请参阅数学类

回答by dashnick

Starting with C# 6.0, you can use

从 C# 6.0 开始,您可以使用

using static System.Math;

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

如果你不想一直写Math.