设置了两个get set变量,name,cost
ArrayList里创建一个新的元素集合。
请问如何能在 Arraylist 里添加name ,cost这两个变量然后现实出来?
ArrayLIst.Add("name{0} cost{1}",name,cost);
貌似这样添加不进去呀,求帮助。
我是个新手。。可能表达上稍微有点问题,希望见谅~

解决方案 »

  1.   

    你可以定义一个包含name,cost这两个属性的对象,然后将对象添加在ArrayLIst里面
      

  2.   

     这个加在哪。。??main函数里吗?
      

  3.   

    ArrayLIst.Add(new {Name=name,Cost=cost});
      

  4.   

    ArrayLIst.Add(name)
    ArrayLIst.Add(cost)就可以啊
    显示的话。ArrayLIst[0]代表name的值,ArrayLIst[1]代表cost的值,根据你的数据类型,可能还需要转换一下数据类型,建议你使用List<string> nameList用来存name,list<string>constList用来存cost。
      

  5.   


    namespace AAA
    {
        public class Product
        {
             public string Name {get;set;}
             public decimal Cost {get;set;}
        }    public class Test1
        {
            static void Main(string[] args)
            {
                 ArrayList al = new ArrayList();
                 al.Add(new Product(){Name="人参果",Cost="8888.88"});
                 al.Add(new Product(){Name="XXX",Cost="6666.66"});
                 //其他操作
            }
        }
    }
      

  6.   

     #6楼 得分:0回复于:2011-09-30 14:27:54C# code
    namespace AAA
    {
        public class Product
        {
             public string Name {get;set;}
             public decimal Cost {get;set;}
        }    public class Test1
        {
            static void Main(string[] args)
            {
                 Dictionary<string, int> a = new ....();
                 a.Add("name", 1);
                 foreach(int val in a.Values)
                 {
                       // Console.
                  }
                 int val = a["name"];
            }
        }
    }
     
     
      

  7.   

    你有两个属性说明是一个复杂的数据类型,那这样可以用类、数组就能加到ArrayList里面去啦