c#从多个方法访问列表

本文关键字:访问 列表 方法 | 更新日期: 2023-09-27 18:10:54

我希望在同一类中访问来自不同方法的列表。有没有一种更简单的方法来访问movieTitle列表,而不需要为每个方法创建一个新的列表?我是否必须在每个方法中引用列表?还是我应该把它们放到一个单独的类里?我的总体目标是有一个选项菜单,它从用户那里获得输入,并根据输入调用一个方法来列出所有电影,将电影添加到列表中,并从列表中随机选择一个电影。

class Program
{
    static void Main(string[] args)
    {
        optionMenu();
        Console.Read();
    }
    static void optionMenu()
    {
        Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
        string ui = Console.ReadLine();
        if (ui == "a") { printNames(); }
        else if (ui == "b") { addMovie(); }
        else if (ui == "b") { randomPickMovie(); }
        else { optionMenu(); }
    }
    static void printNames()
    {
        List<string> movieTitles = new List<string>();
        /*list.....
        / movieTitles.Add("Jurassic Park");
        /..........
        /..........
        */..........

        Console.WriteLine("Movies in your list...");
        for (int i = 0; i < movieTitles.Count;i++)
        {
            Console.WriteLine("'t-" + movieTitles[i]);
        }
    } 
    static void addMovie()
    {
        Console.WriteLine("Enter a title:");
        string newTitle = Console.ReadLine();
        //I can't say...
        //movieTitles.Add(newTitle);
        //...? do I need to make an instance of the list?
    }
    static void randomPickMovie()
    {
        Random r = new Random();
        int random = r.Next();
        Console.WriteLine(movieTitle[random]);
        //same here. How do I access the movie titles in the printName()                 method so
        //I can randomly pick a movie from the list?
    }
}

c#从多个方法访问列表

下面是让电影列表共享的一种方法。这将列表声明并初始化为类的静态成员(而不是方法中的局部变量)。

这种方法对于简单的程序很有效,但是在大型程序中使用全局状态可能会有问题,因为很难看到哪个方法影响哪个全局状态,因此很容易出现bug。

class Program
{
    static void Main(string[] args)
    {
        ...
    }
    static List<string> movieTitles = new List<string>(); 
    static void optionMenu()
    {
        ...
    }
    static void printNames()
    {
        Console.WriteLine("Movies in your list...");
        for (int i = 0; i < movieTitles.Count;i++)
        {
            Console.WriteLine("'t-" + movieTitles[i]);
        }
    } 
    static void addMovie()
    {
        movieTitles.Add(newTitle);
    }
    static void randomPickMovie()
    {
        ...
    }
}
另一种方法是将数据从一个方法传递到另一个方法。这使得更容易看到哪些方法使用了movieList。它还允许我们指定额外的限制,例如,你可以看到printNames只需要列表的只读版本,这样你就知道printNames不能修改列表。这种方法需要做更多的工作,但这是一个好习惯,因为从长远来看,它可以减少bug。
class Program
{
    static void Main(string[] args)
    {
        ...
    }
    static void optionMenu()
    {
        List<string> movieTitles = new List<string>(); 
        string ui = Console.ReadLine();
        if (ui == "a") { printNames(movieTitles); }
        else if (ui == "b") { addMovie(movieTitles); }
        else if (ui == "b") { randomPickMovie(movieTitles); }
        else { optionMenu(); }
    }
    static void printNames(IReadOnlyList<string> movieTitles)
    {
        Console.WriteLine("Movies in your list...");
        for (int i = 0; i < movieTitles.Count;i++)
        {
            Console.WriteLine("'t-" + movieTitles[i]);
        }
    } 
    static void addMovie(List<string> movieTitles)
    {
        movieTitles.Add(newTitle);
    }
    static void randomPickMovie(List<string> movieTitles)
    {
        ...
    }
}

请参阅https://stackoverflow.com/questions/13295319/instance-field-vs-passing-method-parameter查看其他用户对哪种方法更好的看法。

就我而言,我更喜欢这样做。

class Program
{
    static void Main(string[] args)
    {
        Movie movie = new Movie();
        int x = 0;
        while (x < 1)
        {
            movie.optionMenu(); Console.Write("Do you want to exit?");
            string response = Console.ReadLine();
            if (response == "Y") { x = 1; }
        }
        Console.Read();
    }
}
class Movie
{
    public List<string> movieTitles { get; set; }
    public Movie()
    {
        movieTitles = new List<string>();
    }
    public void optionMenu()
    {
        Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
        string ui = Console.ReadLine();
        if (ui == "a") { printNames(); }
        else if (ui == "b") { addMovie(); }
        else if (ui == "c") { randomPickMovie(); }
        else { optionMenu(); }
    }
    public void printNames()
    {
        Console.WriteLine("Movies in your list...");
        for (int i = 0; i < movieTitles.Count; i++)
        {
            Console.WriteLine("'t-" + movieTitles[i]);
        }
    }
    public void addMovie()
    {
        Console.WriteLine("Enter a title:");
        string newTitle = Console.ReadLine();
        if (newTitle != "")
        {
            movieTitles.Add(newTitle);
            Console.WriteLine("New movie successfully added!");
        }
        else
        {
            Console.WriteLine("Cannot add empty movie. Add movie failed.");
        }
    }
    public void randomPickMovie()
    {
        if (movieTitles.Count != 0)
        {
            Random r = new Random();
            int random = r.Next(0, movieTitles.Count - 1);
            Console.WriteLine(movieTitles[random]);
        }
        else { Console.WriteLine("Movie list is empty."); }
    }
}

你所有问题的答案是创建类型为List<string>MovieTitles属性,并像这样访问它:

class Program
{
    static void Main(string[] args)
    {
        optionMenu();
        Console.Read();
    }
    static List<string> movieTitles;
    static List<string> MovieTitles
    {
        get
        {
            if (movieTitles == null)
                CreateMoviesList();
            return movieTitles;
        }
    }
    static void CreateMoviesList()
    {
        movieTitles = new List<string>();
        /*list.....
        / movieTitles.Add("Jurassic Park");
        /..........
        /..........
        */
    }
    static void optionMenu()
    {
        Console.Write("(a)LIST MOVIES|(b)ADD Movie|(c)RANDOM MOVIE");
        string ui = Console.ReadLine();
        if (ui == "a") { printNames(); }
        else if (ui == "b") { addMovie(); }
        else if (ui == "b") { randomPickMovie(); }
        else { optionMenu(); }
    }
    static void printNames()
    {
        Console.WriteLine("Movies in your list...");
        for (int i = 0; i < MovieTitles.Count; i++)
        {
            Console.WriteLine("'t-" + movieTitles[i]);
        }
    }
    static void addMovie()
    {
        Console.WriteLine("Enter a title:");
        string newTitle = Console.ReadLine();
        MovieTitles.Add(newTitle);
    }
    static void randomPickMovie()
    {
        Random r = new Random();
        int random = r.Next();
        Console.WriteLine(MovieTitles[random]);
    }
}

CreateMoviesList()创建电影列表只有一次,可以用来打印电影,随机选择和添加电影以后。