【问题标题】:How to fix C# Jagged arrays Type Conversion Error如何修复 C# 锯齿状数组类型转换错误
【发布时间】:2019-01-30 04:06:20
【问题描述】:

这是来自 MS 在 edx 上的 C# 数据结构和算法课程。

"锯齿状数组 锯齿状数组只是一个数组的数组,每个数组的大小可以变化。”

int[][] jaggedArray = new int[10][];

jaggedArray[0] = new Type[5];//cannot implicitly convert type 'system.Type[]' to 'int[]'

jaggedArray[1] = new Type[7];//cannot implicitly convert type 'system.Type[]' to 'int[]'

jaggedArray[9] = new Type[21];//cannot implicitly convert type 'system.Type[]' to 'int[]'

预期结果:没有错误

实际结果:无法将类型'system.Type[]'隐式转换为'int[]'

请帮忙。

更新: 我是 C# 新手,我认为课程中有错误:https://courses.edx.org/courses/course-v1:Microsoft+DEV204.3x+1T2019/courseware/3544566571fe4074b46541d73149ad8d/c97ee813019947a0b2ba0dfb1f60b132/1?activate_block_id=block-v1%3AMicrosoft%2BDEV204.3x%2B1T2019%2Btype%40vertical%2Bblock%4008d10138ab7542969703766ee6eb1b03

我会向课程报告。

感谢您的评论和回答。他们提供帮助。

【问题讨论】:

  • 您应该声明具有相同类型的锯齿状数组元素,例如jaggedArray[0] = new int[5]。请记住 System.Type != int

标签: c# jagged-arrays


【解决方案1】:

正如评论所说,System.Type 与 int 不同,我认为您想要类似:

Type[][] jaggedArray = new Type[10][];
jaggedArray[0] = new Type[5];
jaggedArray[1] = new Type[7];
jaggedArray[9] = new Type[21];

【讨论】:

    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2014-10-16
    • 2023-03-03
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多