我正在尝试创建一个将列表写入文件的过程,但我不确定如何引用列表,以便该过程可以访问它并写入文件
写入文件过程:它是DinoList的问题在当前上下文中不存在,所以我需要引用它,我不确定该怎么做。
static void WriteToFile()
{
FileStream fileStream = File.OpenWrite("E:/Work/Dinosaur.txt");
BinaryWriter writer = new BinaryWriter(fileStream);
foreach (Dinosaur d in DinoList)
{
writer.Write(d.Name);
writer.Write(d.Classification);
writer.Write(d.Sex);
}
writer.Close();
}
列表当前所在的过程,如果我将其移动到 main,我认为我也需要在此过程中引用它,以便此过程仍然有效。
static void PrintAllDino()
{
LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();
Dinosaur Dino1 = new Dinosaur();
Dino1.Name = "Tyrannosaurus Rex";
Dino1.Classification = "Carnivorous";
Dino1.Sex = 'M';
Dinosaur Dino2 = new Dinosaur();
Dino2.Name = "Velociraptor";
Dino2.Classification = "Carnivorous";
Dino2.Sex = 'F';
Dinosaur Dino3 = new Dinosaur();
Dino3.Name = "Procompsognathus";
Dino3.Classification = "Carnivorous";
Dino3.Sex = 'M';
void printList()
{
Console.WriteLine("The current dinosaurs stored in the list are: ");
Console.WriteLine("n");
foreach (Dinosaur d in DinoList)
{
Console.WriteLine("Name: " + d.Name);
Console.WriteLine("Classification: " + d.Classification);
Console.WriteLine("Sex " + d.Sex);
Console.WriteLine("n");
}
}
DinoList.AddLast(Dino1);
DinoList.AddLast(Dino2);
DinoList.AddLast(Dino3);
printList();
Console.WriteLine(DinoList.Count);
Console.WriteLine("Enter Dinosaur Name: ");
static void MainMenuSelection(int selection)
{
switch (selection)
{
case 1:
Console.WriteLine("");
Console.WriteLine("You have selected to create a new dinosaur.");
DinoInserstionMenu();
break;
case 2:
Console.WriteLine("");
Console.WriteLine("You have selected to remove a dinosaur from the list.");
Console.WriteLine("");
break;
case 3:
Console.WriteLine("");
Console.WriteLine("You have selected to display a specific dinosaurs information.");
Console.WriteLine("");
break;
case 4:
Console.WriteLine("");
Console.WriteLine("You have selected to print out all the dinosaur details stored in the list.");
PrintAllDino();
Console.WriteLine("");
break;
case 5:
Console.WriteLine("");
Console.WriteLine("You have selected to load from a file.");
Console.WriteLine("");
break;
case 6:
Console.WriteLine("");
Console.WriteLine("You have selected to save to a file.");
WriteToFile(DinoList);
DisplayMainMenu();
Console.WriteLine("");
break;
您可以将DinoList实例化为静态变量,并在程序中访问它。在任何函数的"外部"初始化它,使其成为全局的。
// This will make it accessible by any method under this class.
public static LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();
public static void Main(string[] args)
{
// Call your Print and Write methods.
return;
}
static void PrintAllDino()
{
// Dont create the DinoList object here, just update DinoList here and Print it.
}
如果不想创建全局变量,可以将 DinoList 作为参数传递。
static void WriteToFile(LinkedList<Dinosaur> DinoList)
{
//do what you need with DinoList
}
// And whereever you are calling WriteToFile, you can pass the DinoList
WriteToFile(DinoList);
下面是一个工作示例
public class Program
{
// This will make it accessible by any method under this class.
public static LinkedList<Dinosaur> DinoList = new LinkedList<Dinosaur>();
public class Dinosaur
{
public string Name { get; set; }
public string Classification { get; set; }
public char Sex { get; set; }
}
public static void Main(string[] args)
{
while (true)
{
int selection = int.Parse(Console.ReadLine());
if (selection == 4)
break;
MainMenuSelection(selection);
}
return;
}
public static void MainMenuSelection(int selection)
{
switch (selection)
{
case 1: // Create Dinos
CreateDinos();
break;
case 2:
PrintAllDinos();
break;
case 3:
WriteToFile();
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
}
public static void CreateDinos()
{
Dinosaur Dino1 = new Dinosaur();
Dino1.Name = "Tyrannosaurus Rex";
Dino1.Classification = "Carnivorous";
Dino1.Sex = 'M';
Dinosaur Dino2 = new Dinosaur();
Dino2.Name = "Velociraptor";
Dino2.Classification = "Carnivorous";
Dino2.Sex = 'F';
Dinosaur Dino3 = new Dinosaur();
Dino3.Name = "Procompsognathus";
Dino3.Classification = "Carnivorous";
Dino3.Sex = 'M';
DinoList.AddLast(Dino1);
DinoList.AddLast(Dino2);
DinoList.AddLast(Dino3);
}
public static void PrintAllDinos()
{
foreach (Dinosaur d in DinoList)
{
Console.WriteLine("Name: " + d.Name);
Console.WriteLine("Classification: " + d.Classification);
Console.WriteLine("Sex " + d.Sex);
Console.WriteLine("n");
}
}
static void WriteToFile()
{
FileStream fileStream = File.OpenWrite("C:\Temp\dinoList.txt");
StreamWriter writer = new StreamWriter(fileStream);
foreach (Dinosaur d in DinoList)
{
writer.Write($"{d.Name},");
writer.Write($"{d.Classification},");
writer.Write(d.Sex.ToString());
writer.WriteLine();
}
writer.Close();
}
}