C# 如何显示作为int[]数组的OrderedDictionary条目值的值?
我不希望在有序字典中创建两个单独的值,并将它们分成单独的条目,但如果有一种方法可以实现我在这里尝试的操作,那就太酷了C# 如何显示作为int[]数组的OrderedDictionary条目值的值?,c#,C#,我不希望在有序字典中创建两个单独的值,并将它们分成单独的条目,但如果有一种方法可以实现我在这里尝试的操作,那就太酷了 public OrderedDictionary spellResults = new OrderedDictionary() { {"Test",new int[]{5,7}} }; public void Main() { //i'm trying to display the integers within "spellResults["Test"]" b
public OrderedDictionary spellResults = new OrderedDictionary()
{
{"Test",new int[]{5,7}}
};
public void Main()
{
//i'm trying to display the integers within "spellResults["Test"]" but i have no idea how i would accomplish this
int[] pval= ((Array)spellResults["Test"]) as int[];
Console.WriteLine(pval[0]+","+pval[1]);
}
编辑:结果证明我的方法也是正确的,但当我第一次使用在线编译器尝试测试OrderedDictionary时,我忽略了将OrderedDictionary放入类中。
哎呀,无论如何谢谢你的回答
获得值后,一个简单的
foreach
循环就足够了,但您可能需要将OrderedDictionary
改为SortedDictionary
更新-更新为在OP评论后使用OrderedDictionary
using System;
using System.Collections.Specialized;
public class Program
{
public static void Main()
{
var spellResults = new OrderedDictionary()
{{"Test", new int[]{5, 7}}};
var nums = spellResults["Test"] as int[];
if (nums != null)
{
foreach (int num in nums)
{
Console.WriteLine(num.ToString());
}
}
}
}
在:获得值后,一个简单的
foreach
循环就足够了,但您可能需要将OrderedDictionary
改为SortedDictionary
更新-更新为在OP评论后使用OrderedDictionary
using System;
using System.Collections.Specialized;
public class Program
{
public static void Main()
{
var spellResults = new OrderedDictionary()
{{"Test", new int[]{5, 7}}};
var nums = spellResults["Test"] as int[];
if (nums != null)
{
foreach (int num in nums)
{
Console.WriteLine(num.ToString());
}
}
}
}
摆弄:
但是,您需要首先检查spellResults[“Test”]
的类型,因为如果到int[]
的转换未成功(即,它将尝试在null
上迭代),将引发NullReferenceException
)。您应该事先执行空检查:
var pval = spellResults["Test"] as int[];
if (pval != null)
{
foreach (var number in pval)
{
Console.WriteLine(number);
}
}
要按原始代码段以CSV格式打印数字,只需将foreach
替换为-这样,如果pval
包含少于2个元素,也可以避免在运行时抛出程序:
if (pval != null)
{
// this is bad, an IndexOutOfRangeException will be thrown
// if pval contains less than 2 elements:
// Console.WriteLine(pval[0] + "," + pval[1]);
// use string.Join instead::
Console.WriteLine(string.Join(",", pval));
}
但是,您需要首先检查spellResults[“Test”]
的类型,因为如果到int[]
的转换未成功(即,它将尝试在null
上迭代),将引发NullReferenceException
)。您应该事先执行空检查:
var pval = spellResults["Test"] as int[];
if (pval != null)
{
foreach (var number in pval)
{
Console.WriteLine(number);
}
}
要按原始代码段以CSV格式打印数字,只需将foreach
替换为-这样,如果pval
包含少于2个元素,也可以避免在运行时抛出程序:
if (pval != null)
{
// this is bad, an IndexOutOfRangeException will be thrown
// if pval contains less than 2 elements:
// Console.WriteLine(pval[0] + "," + pval[1]);
// use string.Join instead::
Console.WriteLine(string.Join(",", pval));
}
将static与spellResults一起使用,因为您使用的是Main方法中的static方法。请使用以下测试代码
public static OrderedDictionary spellResults = new OrderedDictionary()
{
{"Test",new int[]{5,7}}
};
static void Main(string[] args)
{
int[] pval = ((Array)spellResults["Test"]) as int[];
Console.WriteLine(pval[0] + "," + pval[1]);
}
输出结果
将static与spellResults一起使用,因为您是在主方法中使用的,该方法称为static方法。请使用以下测试代码
public static OrderedDictionary spellResults = new OrderedDictionary()
{
{"Test",new int[]{5,7}}
};
static void Main(string[] args)
{
int[] pval = ((Array)spellResults["Test"]) as int[];
Console.WriteLine(pval[0] + "," + pval[1]);
}
输出结果
以后可能不需要强制转换,但需要将字典的键值类型分别限制为
string
和int[]
。我依赖使用OrderedDictionary类来实现这一点,因为真正的“spellResults”条目中的其余值不仅仅是int[]var nums=拼写结果[“测试”];foreach(int[]nums中的int num){Console.WriteLine(num.ToString());}`不能与OrderedDictionary一起使用。@DED-出现了什么错误?我在.NETFiddle中试过,它似乎很管用@垃圾箱0x-谢谢你的评论。如果使用OrderDictionary,值将是Object类型,不是吗?那么在这种情况下,我们需要转换为int[],不是吗?@nosaid编译器是正确的,但正如OP评论的那样,“real拼写结果
条目中的其余值不仅仅是int[]
”,这使得排序字典
不实用,因为它只能存储int[]
s。这可能以后不需要强制转换,但需要将字典的键值类型分别限制为string
和int[]
。我依赖于使用OrderedDictionary类来实现这一点,因为真正的“spellResults”条目中的其余值不仅仅是int[]var nums=拼写结果[“测试”];foreach(int[]nums中的int num){Console.WriteLine(num.ToString());}`不能与OrderedDictionary一起使用。@DED-出现了什么错误?我在.NETFiddle中试过,它似乎很管用@垃圾箱0x-谢谢你的评论。如果使用OrderDictionary,值将是Object类型,不是吗?那么在这种情况下,我们需要转换为int[],不是吗?@nosaid编译器是正确的,但正如OP评论的那样,“real拼写结果
条目中的其余值不仅仅是int[]
”,这使得排序字典
不实用,因为它只能存储int[]
s。我刚刚意识到我使用的在线编译器没有跟踪我的错误,所以我忘了将字典放入类中进行测试。谢谢。此答案不必要地将spellResults[“Test”]
强制转换为Array
,并且如果pval
包含少于2个元素,则很容易出现此问题。出于我的目的,将始终有两个值。是的。我刚刚意识到我使用的在线编译器没有跟踪我的错误,所以我忘了把字典放在一个类中进行测试。谢谢。此答案不必要地将spellResults[“Test”]
强制转换为Array
,并且如果pval
包含少于2个元素,则很容易出现此问题。出于我的目的,将始终有两个值。所以是的。