Bora
Bora

Reputation: 149

How to add multiple strings to List<string[]>?

I want to add two strings to a string[] list without using a string array. I used a string array named str but I want to add d.Name and d.AvailableFreeSpace directly to list. Is there a way to do this?

public static List<string[]> GetReadyDrives()
{
    DriveInfo[] drives = DriveInfo.GetDrives();
    List<DriveInfo> readyDrives = new List<DriveInfo>();
    List<string[]> parsedReadyDrives = new List<string[]>();

    for (int i = 0; i < drives.Length; i++)
    {
        if (drives[i].IsReady)
        {
             readyDrives.Add(drives[i]);
        }
    }
    foreach (DriveInfo d in readyDrives)
    {
        string[] str=new string[2];
        str[0] = d.Name;
        str[1] = d.AvailableFreeSpace.ToString();
        parsedReadyDrives.Add(str);
    }
    return parsedReadyDrives;
}

Upvotes: 3

Views: 18998

Answers (6)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

You can do it with single LINQ query:

public static List<string[]> GetReadyDrives()
{
    return DriveInfo.GetDrives()
        .Where(d => d.IsReady)
        .Select(d => new string[] { d.Name, d.AvailableFreeSpace.ToString() })
        .ToList();
}

UPDATE: I'd split code which finds ready drives and code which prepares what to write to file. In this case I don't need to look inside method to understand what contained in string array:

public static IEnumerable<DriveInfo> GetReadyDrives()
{
    return DriveInfo.GetDrives()
        .Where(d => d.IsReady);
}

Then just write what you need:

foreach(var drive in GetReadyDrives())
    WriteToFile(drive.Name, drive.AvailableFreeSpace);

Or even this way (but I like more option with descriptive method name):

foreach(var drive in DriveInfo.GetDrives().Where(d => d.IsReady))
    WriteToFile(drive.Name, drive.AvailableFreeSpace);

Upvotes: 0

Omar
Omar

Reputation: 16623

Yes you can do:

parsedReadyDrives.Add(new string[]{d.Name, d.AvailableFreeSpace.ToString()});

So try to use some LINQ. Instead of your code try this to return what you want:

return DriveInfo.GetDrives().Where(x => x.IsReady).Select(x => new string[]{x.Name, x.AvailableFreeSpace.ToString()}.ToList();

Upvotes: 0

Roger Lipscombe
Roger Lipscombe

Reputation: 91825

public static List<string[]> GetReadyDrives()
{
    return DriveInfo.GetDrives()
        .Where(d => d.IsReady)
        .Select(d => new[] { d.Name, d.AvailableFreeSpace.ToString() })
        .ToList();
}

...but, to be honest, you'd be better off doing this:

class ReadyDriveInfo
{
    public string Name { get; set; }
    public string AvailableFreeSpace { get; set; }
}

public static List<ReadyDriveInfo> GetReadyDrives()
{
    return DriveInfo.GetDrives()
        .Where(d => d.IsReady)
        .Select(d => new ReadyDriveInfo
            {
                Name = d.Name,
                AvailableFreeSpace = d.AvailableFreeSpace.ToString()
            })
        .ToList();
}

... but, even there, why do you want the free space as a string?

Upvotes: 3

Alex Florescu
Alex Florescu

Reputation: 5151

Your list is composed of string arrays, so no, you can't add something to the list that is not a string array.

You can create an object composed of two strings, if that makes more sense for what you're trying to do, but you'd still have to initialize that object before adding it.

Upvotes: 0

jason
jason

Reputation: 241583

Every element of a List<string[]> is an instance of string[]. So if you want to add strings individually, you can't. But you can add them as the single element in a single-element instance of string[]. Thus:

parsedReadyDrives.Add(new[] { d.Name });
parsedReadyDrives.Add(new[] { d.AvailableFreeSpace.ToString());

If you want them as the two elements of a two-element instance of string[], you'd say:

parsedReadyDrives.Add(new[] { d.Name, d.AvailableFreeSpace.ToString() });

Frankly, I think passing around a List<string[]> is really nasty. One major concern is that you're placing a very heavy burden on your callers to intimately know the structure of the List<string[]> and what each element of each element means. Additionally, it's not robust to change (you have a maintenance nightmare if you want to change the meaning of any single one element of any element in the List<string[]> or if you want to add additional elements. You might want to consider a more formal data structure that encapsulates your concerns more appropriately.

Upvotes: 2

Dave Bish
Dave Bish

Reputation: 19646

Can you not just do this?

parsedReadyDrives.Add(new []{d.Name, d.AvailableFreeSpace.ToString()});

It's just syntactic sugar, though.

Upvotes: 0

Related Questions