如果我想在“添加用户”类中注册一个用户,然后在另一个类“列出用户”中列出所有注册用户,代码应该如何?
我有 Person 类
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
然后我有添加/注册窗口
class AddView
{
Dictionary<string, Person> _persons = new Dictionary<string, Person>();
public void Display()
{
Console.WriteLine("Firstname");
var firstname = Console.ReadLine();
Console.WriteLine("Lastname");
var lastname = Console.ReadLine();
var per = new Person(firstname,lastname);
_persons.Add(per.FirstName, per);
Console.WriteLine("added");
Console.ReadKey();
var main = new MainView();
main.Display();
}
}
然后我想显示我在不同窗口/类中添加的所有内容
class ListView
{
public void Display()
{
出现了这个问题,我无法访问我在 Add Person 类中注册的数据
foreach (var p in _persons)
{
Console.WriteLine(p.Value.FirstName);
}
}
}