arrayLists 中的 C# 对象 [英] C# objects in arrayLists

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

问题描述

我正在 C# 中使用 ArrayList,我想知道如何将对象添加到 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 - 你真的应该避免在新代码中使用 ArrayLists.使用通用集合类型List<;> 代替.

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.

List中添加、删除和替换项目的操作非常简单.

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

假设您有一些假设类型Animal,您将其实例存储在列表中:

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

ListArrayList 的公共接口实际上非常相似.主要区别在于 ArrayList 只能存储 object 引用,因为它是在 .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 - List 由于是 通用类.另一个问题是 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 中的任意类型的影响,或者使用反射和运行时类型检查以转换存储的值.List 通过允许 编译器 帮助验证集合中只存储了适当的类型(与类型参数 T 匹配的类型),避免了这两个问题 in List).

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.

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

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