如何将通用数组写入 CSV 文件? [英] How can I write a general Array to CSV file?

查看:32
本文介绍了如何将通用数组写入 CSV 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 C# 中有一个对象数组,我想将它写入一个 CSV 格式的文件中.
假设每个对象都有一个ToString()方法,就是我要打印的.

Assume that I have an Array of objects in C#, and I want to write it to a file in CSV format.
Assume that each object has a ToString() method, which is what I want to be printed.

目前我正在使用此代码:

Currently I am using this code:

public static void dumpArray(Array arr, string fileName)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName))
    {
        foreach (Object obj in arr)
        {
             file.Write(obj.ToString()+",");
        }
    }
}

C# 框架中是否有内置的东西,或者您认为有更好的方法吗?

Is there anything built in C# framework, or do you think that there is a better way?

推荐答案

您可以更改方法以使用 C# 泛型 并使用描述性函数和变量名称.这基本上会导致相同的行为,除了值类型避免了 装箱.

You could change your method to use C# Generics and use descriptive function and variable names. This essentially results in the same behaviour except that boxing is avoided for value types.

public static void SaveArrayAsCSV<T>(T[] arrayToSave, string fileName)
{
    using (StreamWriter file = new StreamWriter(fileName))
    {
        foreach (T item in arrayToSave)
        {
            file.Write(item + ",");
        }
    }
}

要对锯齿状数组执行相同操作(假设它们只包含一层嵌套),您可以使用此重载(不推荐,见下文!):

to do the same with jagged Arrays (assuming they only ever contain one level of nesting) you could use this overload (not recommended, see below!):

public static void SaveArrayAsCSV<T>(T[][] jaggedArrayToSave, string fileName)
{
    using (StreamWriter file = new StreamWriter(fileName))
    {
        foreach (T[] array in jaggedArrayToSave)
        {
            foreach (T item in array)
            {
               file.Write(item + ",");
            }
            file.Write(Environment.NewLine);
        }
    }
}

编辑 - 关于重复:

当然,上述解决方案是次优的,因为它仅适用于某些特定场景(我们只有一层嵌套).不幸的是,如果重构我们的代码,我们必须停止使用泛型,因为我们的输入数组现在可能包含类型 T 或类型 T[] 的元素.因此,我们提出了以下代码,这是首选解决方案,因为虽然它重新引入了装箱,但可读性更高,冗余更少,适用于更广泛的场景:

EDIT - Regarding duplication:

The above solution is, of course, sub-optimal in that it only works in a some specific scenarios (where we have exactly one level of nesting). Unfortunately, if refactor our code, we have to stop using generics because our input array might now contain elements of Type Tor of Type T[]. Therefore, we come up with the following code, which is the preferred solution because, although it reintroduces boxing, is more readable, less redundant, and works for a wider range of scenarios:

public static void SaveArrayAsCSV(Array arrayToSave, string fileName)
{
    using (StreamWriter file = new StreamWriter(fileName))
    {
        WriteItemsToFile(arrayToSave, file);
    }
}

private static void WriteItemsToFile(Array items, TextWriter file)
{
    foreach (object item in items)
    {
        if (item is Array)
        {
            WriteItemsToFile(item as Array, file);
            file.Write(Environment.NewLine);
        }
        else file.Write(item + ",");   
    }
}

这篇关于如何将通用数组写入 CSV 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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