Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6250707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:28:36+00:00 2026-05-24T13:28:36+00:00

I am using a foreach loop and Directory.GetFiles() to collect some information about the

  • 0

I am using a foreach loop and Directory.GetFiles() to collect some information about the files in a directory. I want to store 3 strings: Filename, FilePath, DirectoryName. I can get this information okay, but I’m wondering what the best way to store the data is.

I want to be able to look up one of strings, and easily find the others. For example, if they are in 3 arrays, I could use a forloop to scan Filename array and find that the entry that I want is at index 45. I can then just use FilePath[45], DirectoryName[45] to get the entries in the other two arrays.

Would this be the most efficient to store these strings? And if so, assuming that in another case, I wasn’t able to get the size of the arrays with the GetFiles() method beforehand, would a List be the suitable alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:28:37+00:00Added an answer on May 24, 2026 at 1:28 pm

    If you’ve got three associated values, encapsulate them in a type. It needn’t be a particularly complicated type, although I’d probably make it immutable – just three properties initialized from a constructor. The trickiest bit is naming it, given that FileInfo is already part of System.IO… But suppose you settled on the (sucky) name of FileData:

    public sealed class FileData
    {
        // Or use private readonly string variables and getter-only properties
        public string FileName { get; private set; }
        public string Path { get; private set; }
        public string DirectoryName { get; private set; }
    
        public FileData(string fileName, string path, string directoryName)
        {
            FileName = fileName;
            Path = path;
            DirectoryName = directoryName;
        }
    
        // Optional: override methods from System.Object.
        // ToString might be really handy; Equals/GetHashCode less likely.
    }
    

    This won’t be quite as efficient (in terms of memory) as three separate arrays, but it would be a darned sight more practical to use, particularly with LINQ. Spend the time that you gain from not dealing with the separate arrays on optimizing bits which really matter. The memory taken by the strings is likely to dwarf the overhead of the container object.

    You could make this a struct instead of a class, which would be slightly more efficient in some ways and slightly less efficient in others. I usually use a class unless I’ve got a good reason not to.

    And yes, I would usually use a List rather than an array – Eric Lippert has some good reasons why…

    One thing that bothers me slightly: are these three pieces of information genuinely orthogonal, or could you construct the full path from the directory name and file name? If so, you should probably only store the first two, and construct the third when you need to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I am using the SSIS Foreach Loop Container to iterate through files with a
I am using a foreach loop within PHP similar to this: foreach ($class->getAttributes() as
So if I am iterating using a foreach loop and I have a function
I'm using a foreach to loop through an IList of objects in a Partial
I know how to loop through items of an array using foreach and append
I am using a <% foreach ...%> loop to list a collection of items
Is there a way to iterate ValueCollection using straight for loop? (not foreach) e.g.
I want to get user names in active directory by using sharepoint webpart. any
I had a question using Perl's readdir(). I want to gather all the files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.