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

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

问题描述

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

I'm working with ArrayList 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,或需要与使用的ArrayList 遗留代码交互 - 你应该真正避免在新的代码中使用的ArrayList 。使用通用集合类型列表与LT;> ; 而不是

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> 是很简单的。

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");



的公共接口列表< T> 的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 - 列表< T> 由于是一个的泛型类。另一个问题是,的ArrayList 不妨碍你在列表中混合不同类型在一起。例如:

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 更脆弱的集合类型,因为调用者必须编写代码要么保护自己免受任意类型存储在的ArrayList ,否则使用反射和运行时类型检查的值转换存储。 列表< T> 通过允许的编译器避免了这两个问题的来帮助验证只有适当的类型存储在集合中(那些符合该在类型参数 T 列表< T>

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#编写代码之前,你应该花时间来的熟悉C#语言和类型系统的基本概念 - 有什么参考价值与类型。什么是原语。什么是仿制药。等,这将有助于确保当你开始写代码,代码做了你需要它做什么。 C#有一个复杂和丰富的类型系统 - 以及框架类的文库。有在语言的核心方面的功底你太深入实际编写代码之前是很重要的。像我上面显示的例子只会让你至今 - 他们已经引进大量的语言概念:变量,构造,泛型,装箱转换,等等

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 C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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