本文介绍了如何在C#中将对象存储在二维数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我想存储对象EX:
在[0,0]中,我想存储单词"document"和数字"1".
我要在[0,1]中存储单词"go"和数字"12".
我不能只在数组[0,0]中存储"go"或"12".

请,如果可以,如何访问[0,1]?

Hi!
I want to store Object EX:
In [0,0], I want store word "document" and number "1".
In [0,1],I want store word "go" and number "12".
I can not store in array [0,0] only "go" or "12".

Please If I can ,How can I access to [0,1]?

推荐答案

private object[,] objAry = new object[1,1];

objAry[0, 0] = "string1";
objAry[0, 1] = "string2";

object o1 = objAry[0, 0];

string newString = o1;

您可以通过几种其他方法在.NET中获得所需的内容,包括使用Dictionary,但是让我们在这里使用Tuple对象看一个简单的解决方案:

There are several alternate ways you could go about getting what you want in .NET, including the use of a Dictionary, but let''s look at a simple solution here using the Tuple object:

private Tuple<string, string> tupleEntryPlaceHolder;

private List<Tuple<string, string>> tupleList = new List<Tuple<string, string>>();

如何将带有一对字符串的新元组添加到列表中:

How do you add a new Tuple with your pair of strings to the List:

tupleEntryPlaceHolder = Tuple.Create("document1", "1");

tupleList.Add(tupleEntryPlaceHolder);

如何访问元组列表中特定项目(如项目#0)中的元素:

How do you access the elements inside a specific item, like item #0, in your List of Tuples:

string result1 = tupleList[0].Item1;

string result2 = tupleList[0].Item2;

为使访问更简单,让''为此定义了一个函数:

To make access a little easier, let''s define a function for that:

private string getTuple(int tupleIndex, bool isItem1)
{
   tupleEntryPlaceHolder = tupleList[tupleIndex];

   return isItem1 ? tupleEntryPlaceHolder .Item1 : tupleEntryPlaceHolder .Item2;
}

现在,您可以像这样访问:

So now, you can access like this:

string result1 = getTuple(0, true);

string result2 = getTuple(0, false);

这里您可以快速,高效地使用解决方案,不需要拆箱"即可将存储的字符串恢复为Type String即可使用.

正如我所提到的,这也可以使用.NET中的其他数据结构对象来完成,例如Dictionary .而且,也许其他人会在此线程上响应另一种解决方案.

顺便说一下,Tuple对象有原始"形式,每个Tuple最多可以处理8个项目,您甚至可以通过在Tuple定义内粘贴另一个Tuple来将Tuple扩展到每个Tuple超出8个项目.请参阅:[ ^ ] .. .和:[ ^ ]

...编辑#2 ...


在考虑是否要使用Dictionary或Tuple或类似的方法实现这样的解决方案时要牢记的一个重要注意事项是可能存在重复键值的问题.

对于通用词典,添加具有重复键的项目将导致类型为''ArgumentException的运行时错误.

是的,您可以破解相当于允许重复键的Dictionary的等效方法,如果您在CodeProject上进行搜索,则会发现一些与此相关的讨论/文章,但是,解决方案很复杂,而且通常是有争议的",例如您将看到是否阅读了注释.

在C-SharpCorner,有两个有趣的系列文章,由Vulpes [ ^ ],[ ^ ],它提供了有关设计重复Key集合的注意事项的良好概述实体,以及一个非常专业的编码和注释源代码示例.

对于元组,添加具有相同Key的项不会导致编译错误或运行时错误,而只会替换该Key项的Value.

最后,对于使用较旧的"数据结构HashTable,还存在一些错误问题,可能会添加重复的键错误,并且还需要将查找结果转换回某些原始"数据类型.

...结束编辑#2 ...

Here you have a solution that will be fast, and efficient, to use, will not require "unboxing" to get your stored strings back into Type String to be able to use them as Strings.

As I mentioned, this could also be done using other data-structure objects in .NET, like a Dictionary<string, string>, for example. And, perhaps, someone else, on this thread will respond with another type of solution.

By the way, there are "raw" forms of the Tuple object that can handle up to eight items per Tuple, and you can even extend a Tuple beyond eight items per Tuple by sticking another Tuple inside the Tuple definition. See:[^] ... and:[^]

... edit #2 ...


An important consideration to keep in mind in considering whether to implement a solution like this, with a Dictionary, or a Tuple, or a whatever, is the issue of possible duplicate key values.

For a generic Dictionary, adding an item with a duplicate Key will cause a run-time error of Type ''ArgumentException.

Yes, you can hack the equivalent of a Dictionary that allows duplicate keys, and if you search on CodeProject, you''ll find some discussions/articles on this, but, the solutions are complex, and often they are "controversial," as you''ll see if you read the comments.

At C-SharpCorner is an interesting two-part series, "A Dictionary class which permits duplicate keys" by Vulpes[^],[^], that provides a good overview on the considerations for design of a duplicate Key collection entity, and a very professionally coded and commented source-code example.

For a Tuple, adding an item with the same Key will not cause a compile error, or run-time error, but will simply replace the Value of that one Key item.

And, finally, for the use of the "older" data-structure, the HashTable, you also have error issues of adding duplicate key errors possible, and, the need to cast the results of a look-up back to some "original" Type.

... end edit #2 ...



这篇关于如何在C#中将对象存储在二维数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:21