我有一個包含多個數字的.txt文件,我希望它讀取文件並輸出數字。但是當它讀取文件時,它會輸出System.String []而不是數字。任何幫助,將不勝感激。從.txt文件讀取輸出System.String []而不是預期的結果c#
class Program
{
static void Main(string[] args)
{
string[] unsorted = System.IO.File.ReadAllLines(@"\University\AlgRetake\Files\WS1_AF.txt");
//Grabs the .txt file and reads line by line
System.Console.WriteLine("Unsorted: ");
foreach (string line in unsorted)
{
Console.WriteLine(unsorted);
}
//outputs the unsorted array
Console.WriteLine("Press any key to exit!");
System.Console.ReadKey();
}
}
你想要寫的是未排序,而不是實際的行更有效輪到你了。由於數組(甚至是String數組)沒有內置的很好的toString實現,它只是打印類型。將它改爲'Console.WriteLine(line)'應該可以解決這個問題。 –