C#类数组和索引,不要在一个类中的多个阵列必须公开? [英] C# class arrays and indexers, do multiple arrays in a class have to be public?

查看:150
本文介绍了C#类数组和索引,不要在一个类中的多个阵列必须公开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这回事持有3个平行阵列的类。 (对于一类分配我们基本上编码一个基本的XML解析器...开始编程类)

I have a class that's going to hold 3 parallel arrays. (For a class assignment we're basically coding a rudimentary xml parser...beginning programming class)

*注意,我在非常基本的OOP这样做。我已经得到具有阵列一个XMLObject类,并保持在一个阵列中的XML元素,该数据值在另一个,和在第三结束元素。我也有一个XMLPARSE对象,进行实际的分析,并存储字符串到他们在不同的阵列,因为它找到它们。我一直在使用.NET的XML东西,这个任务forbiddin,先后被字节字节读取。

*Note, I'm doing this in very basic OOP. I've got an XMLObject class which has the arrays, and holds the xml elements in one array, the data values in another, and the ending elements in the third. I've also got an XMLParse object that does the actual parsing, and stores the strings to their various arrays as it finds them. I've been forbiddin from using .net's xml stuff for this assignment, has to be a byte by byte read in.

现在我在读MSDN上关于索引,并按照我的理解,我可以只使用一个索引有一个数组(因为这对性能接收参数的唯一途径),或者我必须让我的阵列公众如此该解析类可以加给他们,主要还是另一类可以从中读出。

Now I was reading on MSDN about indexers, and as I understand it, I can either only have one array using an indexer(since that's the only way for properties to receive parameters), or I have to make my arrays public so that the parse class can add to them, and main or another class can read from them.

难道我有这个权利还是我失去了一些东西/不理解怎么去和另一组一类数组?

Do I have that right or am I missing something/not understanding how to get and set arrays of one class from another?

做同样的去列表呢?

推荐答案

如果我理解你的问题,你想对你的班里很多索引器将返回从该类持有阵列的各种元素。

If I understand your question you want to have many indexers on your class which will return the various elements from the arrays that the class holds.

可以有许多索引器,但只有当由每一个索引所使用的类型是不同的。所以,你可以有通过 INT 的索引和索引器字符串,而不是2索引通过 INT

you can have many indexers, but only if the type used by each indexer is different. So you could have an indexer by int and an indexer by string, but not 2 indexers by int.

从事物的声音,你将无法使用索引来访问你所有的类持有的价值,因为他们可能会都要用 INT

From the sounds of things you won't be able to use indexers to access all the values your class holds, as they will probably all want to use int.

公开揭露阵列是一种选择,但你也可以阅读每一件事情提供不同的方法,让你可以有 GetXmlElement(INT指数) GetDataValue(INT指数) GetEndingElement(INT指数)来提供访问数组的内容。

Publicly exposing the arrays is one option, but you could also provide different methods for reading each thing, so you could have GetXmlElement(int index), GetDataValue(int index) and GetEndingElement(int index) to provide access to the contents of the arrays.

另一种选择是存储在阵列中的数据在内部,但接受并返回其一起捆绑的所有数据的类。这样,你可以有它返回的所有数据,所有数据将在它的元素,数据值和最终元件的单个对象的单个索引。

Another option would be to store the data in arrays internally, but accept and return a class which bundled up all the data together. This way you could have a single indexer which returned all the data, as all the data would be a single object with the element, data value and end element in it.

您必须提供把数据和潜在的拆卸和更换,以及类似的方法。无论你想这样做,只是OPR如果exopse要能够行使控制权,将/访问/删除/阵列或不改变底层阵列/列表取决于。

you would have to provide similar methods for adding data and potentially removing and changing as well. Whether you want to do this, opr just exopse the underlying arrays/lists depends on if you want to be able to exercise control over the adding/accessing/deleting/changing of the arrays or not.

这篇关于C#类数组和索引,不要在一个类中的多个阵列必须公开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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