如何将链表作为参数传递给方法以及如何调用它?

时间:2014-09-24 14:58:20

标签: c# linked-list

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作为参数才能在函数中使用它?如果是,那么如何,如果没有,有什么替代方案?

1 个答案:

答案 0 :(得分:1)

你想要这样的事吗?

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);

    }
}