C# 添加方法
有没有可能有一个方法可以添加整个对象及其属性,然后在主函数中调用该方法 我需要使用下面提供的列表来实施案例3。但是,我也不想把C# 添加方法,c#,list,C#,List,有没有可能有一个方法可以添加整个对象及其属性,然后在主函数中调用该方法 我需要使用下面提供的列表来实施案例3。但是,我也不想把List st=newlist();在WriteToFile()和AddStudent()中 例如,我有以下列表: static void Main(string[] args) { List<Student> st = new List<Student>(); Student st1 = new S
List st=newlist()
;在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);
}
static void Main(字符串[]args)
{
List st=新列表();
学生st1=新生(1,“迈克”,“自由职业者”,9);
st1.CalculateSalary();
学生st2=新生(2,“鲍勃”,“首席执行官”,7);
st2.CalculateSalary();
学生st3=新生(3,“蒂娜”,“首席执行官”,3);
st3.CalculateSalary();
做
{
Console.WriteLine(“===========================================”);
Console.WriteLine(“1.写入文件”);
Console.WriteLine(“2.从文件读取”);
Console.WriteLine(“3.添加学生”);
Console.WriteLine(“===========================================”);
answer=Convert.ToInt32(Console.ReadLine());
开关(应答)
{
案例1:
WriteToFile(文件路径);
WriteLine(“文件已创建!”);
打破
案例2:
ReadFromFile(文件路径);
打破
案例3:
控制台.WriteLine(“添加学生”);
打破
}
}while(答案=4);
}
任何帮助都将不胜感激。当然,您可以创建一个方法,接收学生列表,从用户那里收集关于新学生的信息,然后将新学生添加到列表中。它还需要确保,如果传入的列表为null,那么它将初始化该列表 我在猜你的学生财产是什么。您可以更改它们以匹配您的
学生
班级
/// <summary>
/// Gets student information from the user and adds a new student to the list
/// </summary>
/// <param name="existingStudents">The list of students to add to</param>
private static void AddStudent(List<Student> existingStudents)
{
// Initialize the list if it's null
if (existingStudents == null) existingStudents = new List<Student>();
int tempInt;
// Get student information from the user
Console.WriteLine("Enter new student information");
do
{
Console.Write(" 1. Enter student Id: ");
} while (!int.TryParse(Console.ReadLine(), out tempInt));
var id = tempInt;
Console.Write(" 2. Enter student Name: ");
var name = Console.ReadLine();
Console.Write(" 3. Enter student Job Title: ");
var jobTitle = Console.ReadLine();
do
{
Console.Write(" 4. Enter student years of service: ");
} while (!int.TryParse(Console.ReadLine(), out tempInt));
var yrsOfService = tempInt;
// Add the new student to the list
existingStudents.Add(new Student(id, name, jobTitle, yrsOfService));
}
请注意,在Main
方法的开头,您从未将硬编码的学生添加到列表中。创建学生后,可能需要添加以下内容:
st.Add(st1);
st.Add(st2);
st.Add(st3);
为什么这个问题被标记为C++,如果它与C++没有任何关系?你是什么意思?为什么这样一个单独的?为什么你会把<代码>列表ST=新的Listar()?代码>在
WriteToFile()
或AddStudent()
中?一种方法可能会将现有学生数据写入文件,另一种方法会将新学生添加到列表中。你的问题毫无意义。还可以显示你的学生
类,以及方法WriteToFile()
和ReadFromFile()
。奇怪的是,您有一个名为CalculateSalary()
的方法和一个类似于Student
类上的JobTitle
的属性。这些听起来更像Employee
方法和属性。谢谢。我还有一个问题。我想从文件中读取元素,当我自动读取它们时,用文件中的数据填充列表。我试过了,但没有成功:publicstaticvoidshowStudents(列出现有学生,字符串文件路径){ReadFromFile(文件路径);foreach(现有学生中的学生stud){//existingStudents.Add(新学生(stud.id,stud.Name,stud.Position,stud.interporation));Console.WriteLine(stud.ToString());}}}下面是ReadFromFile函数:公共静态void ReadFromFile(字符串文件路径){StreamReader reader=newstreamreader(文件路径);而(!reader.EndOfStream){Console.WriteLine(reader.ReadLine()));}reader.Close();}好的。谢谢我会分开邮寄。
st.Add(st1);
st.Add(st2);
st.Add(st3);