如何将链表作为参数传递到方法中以及如何调用它
本文关键字:何调用 调用 方法 链表 参数传递 | 更新日期: 2023-09-27 18:24:10
class Program
{
public string name;
public string cell;
public Program(string a1,string c1)
{
name = a1;
cell = c1;
}
public void DetailAccept()
{
Console.WriteLine("enter name :");
name = Console.ReadLine();
Console.WriteLine("enter cell :");
cell = Console.ReadLine();
}
public string getName()
{
return name;
}
public string getCellNo()
{ return cell; }
public void AddL(string a, string b, LinkedList<Program> linked)
{
Program p1 = new Program(a, b);
linked.AddLast(p1);
}
public void menu()
{
Console.WriteLine("'n Choose what to do :'n1. Add new Entry'n2. Search Phone'n3.quit'n");
int choice = Convert.ToInt16(Console.ReadLine());
switch(choice)
{
case 1: DetailAccept();
AddL(getName(), getCellNo(), linked); // here is the problem .
}
}
}
我是否需要将LinkedList
作为参数传递才能在函数中使用它? 如果是,那么如何,如果不是,还有什么替代方案?
你打算这样的事情吗?
private LnkedList<Program> _Programs = new LinkedList<Program>();
public void menu()
{
Console.WriteLine("'n Choose what to do :'n1. Add new Entry'n2. Search Phone'n3.quit'n");
int choice = Convert.ToInt16(Console.ReadLine());
switch(choice)
{
case 1: DetailAccept();
AddL(getName(), getCellNo(), _Programs);
}
}