有沒有可能有一個方法,可以添加一個完整的對象的屬性,然後調用主函數內的方法?添加一個方法
我需要採取行動案例3使用我提供下面的列表。但是,我也不想把List<Student> st = new List<Student>()
;都在WriteToFile()
和AddStudent()
。
例如,我有一個列表:
static void Main(string[] args)
{
List<Student> st = new List<Student>();
Student st1 = new Student(1, "Mike", "Freelancer", 9);
st1.CalculateSalary();
Student st2 = new Student(2, "Bob", "CEO", 7);
st2.CalculateSalary();
Student st3 = new Student(3, "Tina", "CEO", 3);
st3.CalculateSalary();
do
{
Console.WriteLine("============================");
Console.WriteLine("1. Write To File");
Console.WriteLine("2. Read From File");
Console.WriteLine("3. Add Student");
Console.WriteLine("============================");
answer = Convert.ToInt32(Console.ReadLine());
switch (answer)
{
case 1:
WriteToFile(filePath);
Console.WriteLine("File Created!");
break;
case 2:
ReadFromFile(filePath);
break;
case 3:
Console.WriteLine("Add Student");
break;
}
} while (answer != 4);
}
任何幫助將非常感激。
爲什麼將此問題標記爲C++,如果它與C++沒有任何關係? –
你是什麼意思由一個單獨的SO? – Kaloyan
爲什麼你會在'WriteToFile()'或'AddStudent()'中列出'List st = new List ();一種方法可能會將現有的學生數據寫入文件,另一種方法會將新學生添加到列表中。你的問題沒有意義。 –