有关使用对象处理ArrayList的问题

时间:2019-08-22 11:08:44

标签: c# object arraylist

我想使用来自另一个类的动态数字ob对象作为我的“公司”类的属性。为此,我想使用ArrayList进行处理。以下哪种方法可行或更好?还是有其他需要改进的想法?

public ArrayList IndustryList = new ArrayList();


//Method return an array of Class IndustryID
IndustryID[] industry = IndustryID.get_IndustryID();

//Did both options work or which is better?
//Option1
IndustryList.Add(industry);

//Option2
foreach (IndustryID industry_obj in industry)
  {
   Industrylist.Add(industry_obj);
  }

1 个答案:

答案 0 :(得分:0)

首先从List<IndustryID>返回此类型IndustryID.get_IndustryID();
然后;

public class Company{
    public List<IndustryID> Industries; 
    Company(List<IndustryID> industries){
        Industries = industries;
    }
}

用法:

 Company obj = new Company(IndustryID.get_IndustryID());
 foreach(var one in obj.Industries){
     Console.WriteLine(one.Name);
 }