Get an array of a field within an array of object containing that field

I have an object like this:

public class SavedFileInfo
{
    public String Address;
    public Int32 DataPort;
    public Int32 AudioPort;
    public String Description;
    public String Filename;
}

And I have an array of those objects like this... SavedFileInfo[] mySFI;

How can I now get an array of the Filename field (such as string[] filenames) from within my collection of the SavedFileInfo objects?

Jon Skeet
people
quotationmark

Personally I'd use LINQ:

var files = mySFI.Select(x => x.Filename)
                 .ToArray();

Alternatively, there's Array.ConvertAll:

var files = Array.ConvertAll(mySFI, x => x.Filename);

As an aside, I would strongly advise you to use properties instead of fields. It's very easy to change your code to use automatically implemented properties:

public class SavedFileInfo
{
    public String Address { get; set; }
    public Int32 DataPort { get; set; }
    public Int32 AudioPort { get; set; }
    public String Description { get; set; }
    public String Filename { get; set; }
}

people

See more on this question at Stackoverflow