[英]C# Math.Sqrt() not working
所以我仍然是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'的定義?
因為您將類命名為Math
,Visual Studio和編譯器不知道您是指您自己的類( Linear.Math
)還是.NET Math
類( System.Math
)。
您可以通過完全限定名稱來指定您希望使用.NET Math
類:
double distanceA = System.Math.Sqrt(totalA);
這指定您要使用System.Math
。 您可以在using指令后添加以下內容,在整個文件中使用它:
using Math = System.Math;
這稱為using alias指令 ,它將告訴編譯器Math
引用System.Math
。
請注意,通常不建議在創建自己的類時使用沖突的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.