首页 > 解决方案 > c#如何调用另一个类的列表

问题描述

我对 c# 有点陌生,正在尝试制作电话簿控制台应用程序。需要一些帮助来弄清楚如何从另一个类调用列表。要理解我的代码:我有一个包含列表的类(ContactsList)。然后我有另一个类(AppOptions),它有一些用户可以选择的选项,一个是 ViewAllContacts。所以我需要知道我需要在 ViewAllContacts 方法中编写什么代码,以及是否需要在其他任何地方进行任何其他更改。

public class ContactsList
    {
        public string contactName { get; set; }
        public int contactNumber { get; set; }

        public void listMethod()
        {
            List<ContactsList> contacts = new List<ContactsList>();
            {
                contacts.Add(new ContactsList { contactName = "John", contactNumber = 01 });
                contacts.Add(new ContactsList { contactName = "Jack", contactNumber = 02 });
                contacts.Add(new ContactsList { contactName = "Jay", contactNumber = 03 });
            }
        }
    }

 class AppOptions
    {
        ContactsList contactList = new ContactsList();
        public void viewAllContacts()
        {
           
        }
    }

我的代码的任何额外改进将不胜感激。谢谢!

编辑:我想要的另一件重要事情是使用 ViewAllContacts 方法遍历列表

标签: c#list

解决方案


  • Contact这是一个联系人(姓名和电话号码)
  • Contacts这是一个集合Contacts

他们来了:

 public class Contact {
   public Contact(string name, string number) {
     if (null == name)
       throw new ArgumentNullException(nameof(name));
     if (null == number) 
       throw new ArgumentNullException(nameof(number));

     //TODO: Add more validation here

     Name = name;
     Number = number;   
   }

   public string Name {get;}
   // Let it be of type string: we can have numbers like "+1(555)123-45-67" 
   public string Number {get;}

   public override string ToString() => $"{Name} {Number}";

   public bool Equals(Contact other) {
     if (ReferenceEquals(this, other))
       return true;
     if (null == other)
       return false;

     return string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(Number, other.Number, StringComparison.OrdinalIgnoreCase);
   }

   public override bool Equals(object obj) => obj is Contact other && Equals(other);

   public override int GetHashCode() => 
     Number?.GetHashCode(StringComparison.OrdinalIgnoreCase) ?? 0;
 }

该集合可以是(如果您愿意List<T>

public class Contacts : IList<Contact> {
  private List<Contact> m_Items = new List<Contact>();

  public Contact this[int index] { 
    get => m_Items[index]; 
    set {
      if (null == value)
        RemoveAt(index);
      else
        m_Items[index] = value;
    } 
  }

  public void Add(Contact item) {
    if (null == item)
      throw new ArgumentNullException(nameof(item));

      m_Items.Add(item);
  }

  public void Insert(int index, Contact item) {
    if (null == item)
      throw new ArgumentNullException(nameof(item));

    m_Items.Insert(index, item);
  }

  public int Count => m_Items.Count;
  public bool IsReadOnly => false;
  public void Clear() => m_Items.Clear();
  public bool Contains(Contact item) => m_Items.Contains(item);
  public void CopyTo(Contact[] array, int arrayIndex) => m_Items.CopyTo(array, arrayIndex);
  public IEnumerator<Contact> GetEnumerator() => m_Items.GetEnumerator();
  public int IndexOf(Contact item) => m_Items.IndexOf(item);
  public bool Remove(Contact item) => m_Items.Remove(item);
  public void RemoveAt(int index) => m_Items.RemoveAt(index);
  IEnumerator IEnumerable.GetEnumerator() => m_Items.GetEnumerator();
}

那么您可以轻松使用它

private Contacts myContacts = new Contacts() {
  new Contact("John", "01"),
  new Contact("Jack", "02"),
  new Contact("Jay", "03"), 
};

...

public void viewAllContacts() {
  foreach (var contact in myContacts)
    Console.WriteLine(contact);           
}

推荐阅读