下在的ArrayList#对象。 [英] C# objects in arrayLists.

查看:112
本文介绍了下在的ArrayList#对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#的ArrayList的工作,我想知道我怎么可以添加对象到一个ArrayList,然后从中获取价值?

I am working with arrayLists in C# and I am wondering how I can add objects to an arrayList and then retrieve the values from it?

总之,我怎么可以添加,删除,编辑从含有类对象的一个​​ArrayList读?

In short, how can I add, delete, edit and read from an Arraylist containing objects of classes?

感恩所有帮助!

推荐答案

除非你是在你的的情况下,必须使用.NET 1.0 / 1.1,或需要与传统code互动使用的ArrayList - 你应该避免在新的code。使用的ArrayList 。使用通用集合类型列表与LT;&GT ; 而不是

Unless you are in a situation where you must use .NET 1.0/1.1, or need to interact with legacy code that uses ArrayList - you should really avoid using ArrayLists in new code. Use the generic collection type List<> instead.

的操作添加,删除,并替换的项目列表与LT; T&GT; 是很简单的。

The operations to add, remove, and replace an item in List<T> are quite straightforward.

假设你有一些的假设的类型动物,其中你会在一个列表存储实例:

Let's say you have some hypothetical type Animal, instances of which you will store in a list:

Animal dog = new Animal("dog");
Animal cat = new Animal("cat");

List<Animal> animalList = new List<Animal>();

// example of adding items to the list
animalList.Add( dog );
animalList.Add( cat );

// example of removing items form the list
animalList.Remove( cat );

// example of replacing an item at a given position
animalList[0] = new Animal("giraffe");

公共接口列表&LT; T&GT; 的ArrayList 其实颇为相似。主要的区别是,ArrayList中只能存储对象引用,因为它是实施之前的.NET泛型支持

The public interfaces for List<T> and ArrayList are actually quite similar. The main difference, is that ArrayList can only store object references since it was implemented before .NET supported generics.

ArrayList listOfObjects = new ArrayList();
int myAge = 207;
listOfObjects.Add( (object)myAge );

在上面的例子中,你的必须的施放类型,如 INT 这是值类型在.NET )对象。这导致装箱转换 - 这副本的int值类型堆新的位置,并把它ArrayList的。 装箱转换的,使用的缺点之一的ArrayList - 列表&LT; T&GT; 由于是一个的泛型类。另一个问题是,的ArrayList 不会从列表中混合不同类型的共同prevent你。例如:

In the example above, you MUST cast types like int (which are value types in .NET) to object. This results in a boxing conversion - which copies the int value type to a new location on the heap, and passes it to ArrayList. Boxing conversions, are one of the disadvantages of using ArrayList - List<T> avoids this by virtue of being a generic class. Another issue is that ArrayList does not prevent you from mixing different types in the list together. For instance:

listOfObjects.Add( (object)myAge );
listOfObjects.Add( "Hello World" );

都允许的。然而,访问一个的ArrayList 的元素时,必须的知道的你试图要检索的类型。这使得的ArrayList 更脆弱的集合类型,因为调用者必须写code要么保护自己免受任意类型存储在的ArrayList ,否则使用反射和运行时类型检查的值转换存储。 列表&LT; T&GT; 通过允许的编译器避免了这两个问题的来帮助验证只有适当的类型存储在集合中(那些符合该类型参数 T 列表&LT; T&GT;

are both allowed. However, when accessing elements of an ArrayList, you must know what type you are trying to retrieve. This makes ArrayList more fragile as a collection type, because the caller must write code to either protect themselves from arbitrary types being stored in the ArrayList, or else use reflection and runtime type checks to convert the values being stored. List<T> avoids both of these problems by allowing the compiler to help verify that only appropriate types are stored in the collection (those that match the type parameter T in List<T>).

有一个很大的更可能被写与馆藏互动 - 而事实上是有的。这里有一个链接只是一个关于这一主题的许多伟大的书籍。 我的建议是,你开始在.NET / C#写code之前,你应该花时间的熟悉C#语言和类型系统的基本概念 - 有什么参考价值与类型。什么是原语。什么是仿制药。等,这将有助于确保当你开始写code时,code做你需要它做什么。 C#有一个复杂和丰富的类型系统 - 以及框架类的文库。有在语言的核心方面的功底你太深入实际写作code之前,这一点很重要。像我上面显示的例子只会让你至今 - 他们已经引进大量的语言概念:变量,构造,泛型,装箱转换等

There's a great deal more that could be written about interacting with collections - and in fact there is. Here's a link to just one of many great books on the subject. My advice would be, before you begin writing code in .NET/C#, you should take the time to familiarize yourself with the basic concepts of the C# language and type system - what are reference vs. value types. What are primitives. What are generics. etc. This will help ensure that when you start writing code, the code does what you need it to do. C# has a sophisticated and rich type system- as well as a vast library of framework classes. It's important to have a good grounding in the core aspects of the language before you get too deep into writing actual code. Examples like those I show above will only get you so far - and they already introduce numerous language concepts: variables, constructors, generics, boxing conversions, etc.

这篇关于下在的ArrayList#对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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