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

  • Home
  • SEARCH
  • 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 328261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:28:43+00:00 2026-05-12T09:28:43+00:00

I wrote these helper functions a few weeks ago and I feel like I’m

  • 0

I wrote these helper functions a few weeks ago and I feel like I’m not taking advantage of some C# language features that would keep me from writing these same loops again for other similar helpers.

Can anyone suggest what I’m missing?

public static IList<string> GetListOfLinesThatContainTextFromList(
        Stream aTextStream, IList<string> aListOfStringsToFind)
{
    IList<string> aList = new List<string>();

    using (var aReader = new StreamReader(aTextStream))
    {
        while (!aReader.EndOfStream)
        {
            var currLine = aReader.ReadLine();

            foreach (var aToken in aListOfStringsToFind)
                if (currLine.Contains(aToken))
                    aList.Add(currLine);
        }
    }

    return aList;
}

public static DataTable GetDataTableFromDelimitedTextFile(
        Stream aTextStream, string aDelimiter)
{
    var aTable = new DataTable();
    Regex aRegEx = new Regex(aDelimiter);

    using (var aReader = new StreamReader(aTextStream))
    {
        while (!aReader.EndOfStream)
        {
            // -snip-
            // build a DataTable based on the textstream infos
        }
    }

    return aTable;
}
  • 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-12T09:28:43+00:00Added an answer on May 12, 2026 at 9:28 am

    I’d use composition here. For example, for the first one, first split out the bit reading lines – and make it lazy…

    public static IEnumerable<string> ReadLines(Stream input)
    {
        // Note: don't close the StreamReader - we don't own the stream!
        StreamReader reader = new StreamReader(input);
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line;
        }
    }
    

    (You can find a more fully-featured version of this in MiscUtil.)

    Now you can use LINQ to filter out lines not in an appropriate set (and I would use a HashSet rather than a list, unless it’s a really short list):

    var acceptedLines = new HashSet<string>();
    // Populate acceptedLines here
    var query = ReadLines(input).Where(line => acceptedLines.Contains(line))
                                .ToList();
    

    You can use the same line-reading method when populating the DataTable. You need to be careful though – this is lazily evaluated so you need to keep the stream open until you’ve finished reading, and then close it yourself. I prefer to pass in something which lets you get at a Stream (or a TextReader) as then the closing logic can be in the ReadLines method.

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

Sidebar

Related Questions

I have made some helper functions that run a simulation using a lot of
Back in VB6, I wrote a few functions that would let me code without
I have been writing some helper functions in Android 3.0's renderscript, and have come
I've got a class with some functions which realistically are just 'helper' methods that
I wrote these lines in My Application start event: var mongo = new Mongo();
I wrote these lines in my code: CFUUIDRef identifier = CFUUIDCreate(NULL); NSString *identifierString =
In serialization mechanism,we are wrote the object into stream using objectinputstream and object outputstream.These
I am porting some queries from Access to T-SQL and those who wrote the
I would like to create a debugging helper function which I can use within
I am programming some 'openvpn-like' thing and thought it would be a good candidate

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.