我为什么能写入CSV文件一般的阵列? [英] How can I write a general Array to CSV file?

查看:93
本文介绍了我为什么能写入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.

目前我使用这个code:

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);
        }
    }
}

修改 - 关于重复数据删除:

上面的解决方案是,当然,次优的,因为它仅在某些特定情况下的工作(其中我们有嵌套正好一个级别)。不幸的是,如果重构我们的code,我们必须停止使用泛型,因为我们的输入数组现在可能包含 T类型的元素或类型的 T [] 。因此,我们拿出下面code,这就是的preferred解决方案,因为尽管它重新引入拳击,更具有可读性,少冗余的,适用于更广泛的场景

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天全站免登陆