如何增加数组C#来结束? [英] How to add to end of array C#?

查看:195
本文介绍了如何增加数组C#来结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加一个新的项目从Windows窗体上的文本框和按钮的结束的ArrayList 它引用的类?

How do I add a new item from a TextBox and Button on a Windows Form at the end of an ArrayList which references a class?

private product[] value = new product[4];

value[1] = new product("One",5)
value[2] = new product("Two",3)
value[3] = new product("Three",8)

工作流程

  • 输入新产品的详细信息到 TextBox1的 TextBox2中 textbox3
  • 当我点击添加新产品被添加到阵列中:

    Workflow

    • Enter new products details into textbox1, textbox2, textbox3
    • When I click Add the new product gets added to the array:

      值[1] =新产品(一,5)
      值[2] =新产品(两,3)
      值[3] =新产品(三,8)
      值[4] =新产品(四,2)

      value[1] = new product("One",5)
      value[2] = new product("Two",3)
      value[3] = new product("Three",8)
      value[4] = new product("Four",2)

      什么是code这样做?

      What is the code for doing this?

      推荐答案

      数组是固定大小的,这意味着你不能比分配在创建时将号码添加更多的元素,如果你需要一个自动调整大小集合,你可以使用名单,其中,T> 的ArrayList

      Arrays are fixed size, which means you can't add more elements than the number allocated at creation time, if you need a auto sizing collection you could use List<T> or an ArrayList

      例如:

      // using collection initializers to add two products at creation time
      List<Product> products = new List<Product>{new Product("One",5), new Product("Two",3) };
      
      // then add more elements as needed
      products.Add(new Product("Three",8));
      

      这篇关于如何增加数组C#来结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆