C# Keep getting“无法将类型从void隐式转换为字符串”

C# Keep getting“无法将类型从void隐式转换为字符串”,c#,C#,所以我想为大学写一个程序。其目的是将学生的姓氏和考试成绩写入文本文件。我一直在犯这个错误。我不知道该怎么办。代码还没有完成,我还需要为它编写其他函数,因此空间是空的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Assignment2 { class

所以我想为大学写一个程序。其目的是将学生的姓氏和考试成绩写入文本文件。我一直在犯这个错误。我不知道该怎么办。代码还没有完成,我还需要为它编写其他函数,因此空间是空的

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

namespace Assignment2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "StudentTestScores.txt";
            StreamWriter SW = File.CreateText(path);

            string sUsrOpt = "";
            byte bNumStudents = 0;

            Console.Write("Enter Amount Of Students In The Class (Max 20): ");
            byte.TryParse(Console.ReadLine(), out bNumStudents);

            string[] sSurnames = new string[bNumStudents];
            float[] fScores = new float[bNumStudents];



            mainMenu(sUsrOpt, sSurnames, fScores, ref bNumStudents, SW);

            Console.WriteLine("Press Any Key To Open File...");
            string temp = "";
            if (File.Exists(path))
            {
                using (StreamWriter sr = File.AppendText(path))
                    for (int i = 0; i < bNumStudents; i++)
                {
                    foreach (var line in (path))
                    {
                        Console.WriteLine("");

                        temp = sr.WriteLine();
                        Console.Write("Surname: {0}", temp);
                        Console.WriteLine("");

                        temp = sr.WriteLine();
                        Console.Write("Score");
                        Console.WriteLine("");
                    }
                    Console.WriteLine("press any key to continue");
                    Console.ReadKey();
                    Console.Clear();
                }

            }




            Console.ReadKey();

            System.Diagnostics.Process.Start(path);

            SW.Close();
        }


        //The menu

        private static void mainMenu(string sUsrOpt, string[] sSurnames, float[] fScores, ref byte bNumStudents, StreamWriter SW)
        {
            Console.WriteLine("\nOption A - Open and display a test\nOption B - Enter and save a test\nOption C - Help\nOption D - Exit");

            Console.Write("Choose An Option: ");
            sUsrOpt = Console.ReadLine();


            switch (sUsrOpt.ToUpper())
            {
                case "A":
                    Opentests(sSurnames, SW);
                    break;
                case "B":
                    EnterAndSaveaTest(fScores, sSurnames, ref bNumStudents);
                    break;
                case "C":
                    Help(fScores, sSurnames, SW);
                    break;
                case "D":
                    Exit();
                    break;

                default:
                    break;

            }
        }
        //Enter and save a test
        private static void EnterAndSaveaTest(float[] fScores, string[] sSurnames, ref byte bNumStudents)
        {
            if (bNumStudents > 20)
            {
                Console.WriteLine("Too High, try again");

            }

            for (int i = 0; i < bNumStudents; i++)
            {
                Console.Write("Enter Surname {0}: ", i + 1);
                sSurnames[i] = Console.ReadLine();

                Console.Write("Enter Score For {0}: ", sSurnames[i]);
                float.TryParse(Console.ReadLine(), out fScores[i]);
            }

            for (int y = 0; y < bNumStudents; y++)
            {
                Console.WriteLine(fScores[y]);
            }

        }

        //Open a test
        private static void Opentests(string[] sSurnames, StreamWriter SW)
        {
            Console.WriteLine("Please input a previously entered surname");


        }


        //Gives help to the user
        private static void Help(float[] fScores, string[] sSurnames, StreamWriter SW)
        {


        }
        //Ends the program
        private static void Exit()
        {
            Console.ReadLine();

        }
    }
}

路径是一个字符串,您需要打开文件,我将使用StreamReader 然后从StreamReader中读取每一行

foreach (var line in (path))
然后,看起来你是在试图阅读一位作家的作品

temp = sr.WriteLine();

WriteLine是一个无效的返回,您不能为某个值分配没有返回的内容。

请不要提供完整的代码库,而只提供与重现问题相关的代码部分。特别是请提供您从何处获得该错误。哪一行给出该错误?