数组初始化器只能在变量或字段初始化器中使用吗?

3
我遇到了以下错误:
数组初始化器只能用于变量或字段初始化。尝试使用新表达式代替。
这是我的代码:
// Declare listbox information array
string [] tablet = new string[]{{"Microsoft Surface  Price: $1,162.99  Screen Size: 10.6 Inches  Storage Capacity: 128 GB"},

                                {"iPad 2 Price: $399.99, Screen Size: 9.7 Inches, Storage Capacity 16 GB"},
                                {"Samsung Galaxy Tab 2 Price: $329.99, Screen Size: 10.1 Inches, Storage Capacity 16 GB"},
                                {"NOOK HD Price: $199.99, Screen Size: 7 Inches, Storage Capacity 8 GB"},
                                {"IdeaTab Price: $149.99, Screen Size: 7 Inches, Storage Capacity: 8 GB"}};

//Array of product prices
int [] tabletPricesArray = new int[]{{"$1,162.99"},
                                       {"$399.99"},
                                       {"$329.99"},
                                       {"$199.99"},
                                       {"$149.99"}};

我不太确定出了什么问题。我对C#比较新手,如果需要任何额外的信息,请告诉我。


1
你是否正在尝试创建二维数组? - lc.
2
您不需要内部花括号。此外,您不能用字符串初始化int。而且,除非你计算了分数,否则你的价格不是整数。建议使用十进制类型来表示货币数量。 - Anton Tykhyy
2个回答

6
一些问题:
问题1:
你在这里创建了一个类型为“int”的数组,却提供了字符串。
  int [] tabletPricesArray = new int[]{"$1,162.99",
                                         "$399.99",
                                         "$329.99",
                                         "$199.99",
                                         "$149.99"};

问题2:
类型为 int 的数组无法存储浮点数,例如价格。请改用 floatdoubledecimal(用于美元)代替。
    decimal[] tabletPricesArray = new decimal[]{1162.99M,
                                                 399.99M,
                                                 329.99M,
                                                 199.99M,
                                                 149.99M};

如果您希望tabletPricesArray仅用于将项目显示为字符串(无计算),则也可以在此处使用字符串数组。
问题3:
每个数组元素中都不需要使用{ }

我想不到还有什么需要说的。 - siride

4
我希望以下内容能够符合您的期望。我已经为您修改了代码。
       // Declare listbox information array
       string[] tablet = new string[]{"Microsoft Surface  Price: $1,162.99  Screen Size: 10.6 Inches  Storage Capacity: 128 GB",
                                      "iPad 2 Price: $399.99, Screen Size: 9.7 Inches, Storage Capacity 16 GB",
                                      "Samsung Galaxy Tab 2 Price: $329.99, Screen Size: 10.1 Inches, Storage Capacity 16 GB",
                                      "NOOK HD Price: $199.99, Screen Size: 7 Inches, Storage Capacity 8 GB",
                                      "IdeaTab Price: $149.99, Screen Size: 7 Inches, Storage Capacity: 8 GB"};

       // Array of product prices
       string[] tabletPricesArray = new string[]{"$1,162.99",
                                                   "$399.99",
                                                   "$329.99",
                                                   "$199.99",
                                                   "$149.99"};

谢谢大家。我的大部分语法错误都已经解决了,只需要进行一些小修补即可。 - Simon Kay
好的,我们很乐意帮助您。我建议您快速参考一些书籍,比如Herbert Schildt的《C# 4.0完全参考手册》,以了解语法。如果您对任何答案感到满意,可以通过点击每个答案旁边的勾选标记来确认它。祝福您。 - Deepak Raj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接