myClass数组可以是集合吗? [英] myClass array can be a collection?
问题描述
我有一个Book类,具有许多属性.我定义了一个Book类数组(Book [] books),以便每次添加或搜索一本书时,我都会有一本书及其所有属性值,但是此数组不是动态的,本身并不关心索引,并且不会像
i have a Book class with many attributes. i defined a Book class array(Book[] books) so that each time i add or search book, i will have a book with all its attributes values but this array is not dynamic, does not care of index byitsself and does not search like collections, i have to code to maintain all these things so is there any way that a Book[] array is collection too?
推荐答案
您可以尝试以下操作. br/>
列表<书> booksList = new List< Book> ;;
booksList = books.ToList();
编辑-请不要通过"pre"/"code"标签进行编辑,否则会干扰代码.
You may try as below.
List<Book> booksList = new List<Book>;
booksList = books.ToList();
Edit - Please don''t edit by "pre"/"code" tag, it distrupting the code.
不是真的,但是Collection可以是Array-如果您定义
Not really, but a Collection can be an Array - if you define a
List<string> myList = new List<string>();
,那么您可以使用myList [6](如果存在),也可以使用 List.ToArray [
then you can use myList[6] provided it exists, or you can convert it to an array with the List.ToArray[^] method.
Alternatively, you can convert an array to a List very easily by using the List.AddRange method - but I would probably start with a list instead.
^ ].数组实现ICollection,IList,IEnumerable,但是添加,插入或删除元素的方法将强制转换为NotSupportedException
,例如IList.Add
.
Yes an array is a collection[^]. Array implements ICollection, IList, IEnumerable, however methods which adds, inserts or removes elements will cast aNotSupportedException
, e.g.IList.Add
.
这篇关于myClass数组可以是集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!