将不同类型的数组存储到锯齿状的数组中 [英] Storing an array of a different type into a jagged array

查看:75
本文介绍了将不同类型的数组存储到锯齿状的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储在锯齿状数组中的数组是否必须为同一类型?例如,我可以将一个整数数组和一个字符串数组存储在一个锯齿状的数组中?

Does an array stored inside a jagged array need to be of the same type? for example can I store an array of ints and an array of strings in one jagged array?

推荐答案

为什么不使用对象?这是一个示例-

Why not use an array of objects ? here is an example-

        var jaggedArray = new object[3];
        jaggedArray[0] = new[] { 1, 2, 3 };
        jaggedArray[1] = new[] { "str", "onemore" };
        jaggedArray[2] = new[] { new { prop = 14 }, new { prop = 12 }, new { prop = 1 } };
        Console.Write(jaggedArray[0].ToString());

复制并复制将其粘贴到控制台应用程序中,并在Console.Write上创建一个断点。您可以在快速监视窗口中看到内置的阵列。

Copy & paste this in a console application and have a breakpoint on Console.Write, you can see the built array in quick watch window.

这篇关于将不同类型的数组存储到锯齿状的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆