C#Math.Sqrt()无效

时间:2014-11-04 02:36:02

标签: c# .net

所以我仍然是C#编程的新手,所以我确定我在这里犯了一个非常简单的错误。我正在使用visual studio来制作一个控制台应用程序,在这个应用程序中,我试图找到一个数字的平方根。我有.NET框架 这是我的计划的顶部:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linear
{
    public static class Math
    {
        static void Main()
        {
          // program in here
        }
     }
}

在程序中,我有一行如下:

double distanceA = Math.Sqrt(totalA);

因此,当我使用VS构建程序时,它告诉我'Linear.Math'不包含'Sqrt'的定义。 为什么这么说呢?我怎样才能给出'Sqrt'的定义?

1 个答案:

答案 0 :(得分:24)

因为您将类命名为Math,所以Visual Studio和编译器不知道您是指自己的类(Linear.Math)还是.NET Math类({ {1}})。

您可以通过完全限定名称来指定您希望使用 .NET System.Math类。:

Math

这表明您想使用double distanceA = System.Math.Sqrt(totalA); 。您可以在using指令后添加以下内容,在整个文件中使用它:

System.Math

这称为 using alias directive ,它会告诉编译器using Math = System.Math; 引用Math

请注意,在创建自己的类时,通常不建议使用冲突的名称。