これを実現するには明らかにさまざまな方法があり、設計要件に基づいています。
ここでは、C# の初心者であり、この旅を始めるための次の 2 つの簡単な方法を検討してください (既存のコードにできるだけ慣れるために、慣用的な C# は意図的に除外しました)。
オプション 1 - パラメータとして渡します。
public List<string> BuildMenuList()
{
List<string> flavors = new List<string>();
flavors.Add("Angus Steakhouse");
flavors.Add("Belly Buster");
flavors.Add("Pizza Bianca");
return flavors;
}
public int GetSizePrices(int num, List<string> menuList)
{
// access to menuList
var cnt = menuList.Count();
}
オプション 2 - プロパティとして利用できるようにする
public class Menu
{
// A public property that is accessible in this class and from the outside
public List<string> MenuList { get; set;}
public Menu()
{
// Populate the property in the constructor of the class
MenuList = new List<string>();
MenuList.Add("Angus Steakhouse");
MenuList.Add("Belly Buster");
MenuList.Add("Pizza Bianca");
}
public int GetSizePrices(int num)
{
// your implementation details here...
return MenuList.Count();
}
}
それが役に立てば幸い。