在 C# 中创建二维数组的字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/638498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a dictionary of two-dimensional arrays in c#
提问by tmcallaghan
I'm trying to create a dictionary of two-dimensional arrays in C#, I can't figure out the proper syntax. I've tried the following to no avail, but it shows what I'm trying to accomplish.
我试图在 C# 中创建一个二维数组字典,我无法弄清楚正确的语法。我已经尝试了以下无济于事,但它显示了我想要完成的事情。
Dictionary dictLocOne = new Dictionary<String,double[][]>();
采纳答案by Gregory A Beamer
A couple of things here:
这里有几件事:
Definition must match initialization. You are definining Dictionary and instantiating Dictionary<TKey, TValue>. What this means, based on what you are saying here:
定义必须匹配初始化。您正在定义 Dictionary 并实例化 Dictionary<TKey, TValue>。这意味着什么,基于你在这里所说的:
Dictionary<string, double[][]> dict = new Dictionary<string, double[][]>();
I assume this is what you want. If so, your code might be something like this:
我假设这就是你想要的。如果是这样,您的代码可能是这样的:
double[] d1 = { 1.0, 2.0 };
double[] d2 = { 3.0, 4.0 };
double[] d3 = { 5.0, 6.0, 7.0 };
double[][] dd1 = { d1 };
double[][] dd2 = { d2, d3 };
Dictionary<string, double[][]> dict = new Dictionary<string, double[][]>();
dict.Add("dd1", dd1);
dict.Add("dd2", dd2);
If that is it, your issue is solved.
如果是这样,你的问题就解决了。
回答by Damien
Just gonna update my answer to include the correct declaration as per other answers:
只是要更新我的答案以包含其他答案的正确声明:
Dictionary<String,double[][]> = new Dictionary<String,double[][]>();
Alsoyours is a array of arrayand not a MultiDimensionalone..Not sure if that's what you want..
Alsoyours是一个数组的数组,而不是一个多维知道这是你想要的one..Not ..
If you want a MultiDimensional Array it's
如果你想要一个多维数组,它是
Dictionary<String,double[,]> = new Dictionary<String,double[,]>();
回答by Sebastian Mach
You also have to fully qualify the type of the variable, not only of what you are going to allocate:
您还必须完全限定变量的类型,而不仅仅是您要分配的类型:
Dictionary<String,double[][]> dictLocOne = new Dictionary<String,double[][]>();
回答by John Feminella
Try
尝试
var dict = new Dictionary<String, double[,]>();
回答by Paul C
Example:
例子:
var d = new Dictionary<string, double[,]>();
var d = new Dictionary<string, double[,]>();
d["foo"] = new[,] { { 0.1, 1.0 }, { 0.2, 2.0 }, { 0.3, 3.0 } };
d["foo"] = new[,] { { 0.1, 1.0 }, { 0.2, 2.0 }, { 0.3, 3.0 } };