如何在 C# 中将 2D 对象数组转换为 2D 字符串数组? [英] How to convert a 2D object array to a 2D string array in C#?

查看:68
本文介绍了如何在 C# 中将 2D 对象数组转换为 2D 字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为值"的二维对象数组.我想将其转换为二维字符串数组.我该怎么做?

I have a 2D array of objects called 'value'. I want to convert it to a 2D string array. How do I do this?

object value; //This is a 2D array of objects
string prop[,]; //This is a 2D string

如果可能的话,我还想知道是否可以将对象转换为

If possible I would also like to know if I can convert the object to

List<List<string>>

直接.

推荐答案

这是您要找的吗?

        string[,] prop; //This is a 2D string
        List<List<string>> mysteryList;

        if (value is object[,])
        {
            object[,] objArray = (object[,])value;

            // Get upper bounds for the array
            int bound0 = objArray.GetUpperBound(0);//index of last element for the given dimension
            int bound1 = objArray.GetUpperBound(1);

            prop = new string[bound0 + 1, bound1 + 1];
            mysteryList = new List<List<string>>();

            for (int i = 0; i <= bound0; i++)
            {
                var temp = new List<string>();

                for (int j = 0; j <= bound1; j++)
                {
                    prop[i, j] = objArray[i, j].ToString();//Do null check and assign 
                    temp.Add(prop[i, j]);
                }
                mysteryList.Add(temp);
            }
        }

这篇关于如何在 C# 中将 2D 对象数组转换为 2D 字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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