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 4267574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:55:06+00:00 2026-05-21T06:55:06+00:00

I have a collection of string in C#. My Code looks like this: string[]

  • 0

I have a collection of string in C#. My Code looks like this:

string[] lines = System.IO.File.ReadAllLines(@"d:\SampleFile.txt");

What i want to do is to find the max length of string in that collection and store it in variable. Currently, i code this manually, like?

int nMaxLengthOfString = 0;
for (int i = 0; i < lines.Length;i++ )
{               
   if (lines[i].Length>nMaxLengthOfString)
   {
      nMaxLengthOfString = lines[i].Length;
   }
}

The code above does the work for me, but i am looking for some built in function in order to maintain efficiency, because there will thousand of line in myfile 🙁

  • 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-21T06:55:06+00:00Added an answer on May 21, 2026 at 6:55 am

    A simpler way with LINQ would be:

    int maxLength = lines.Max(x => x.Length);
    

    Note that if you’re using .NET 4, you don’t need to read all the lines into an array first, if you don’t need them later:

    // Note call to ReadLines rather than ReadAllLines.
    int maxLength = File.ReadLines(filename).Max(x => x.Length);
    

    (If you’re not using .NET 4, it’s easy to write the equivalent of File.ReadLines.)

    That will be more efficient in terms of memory, but fundamentally you will have to read every line from disk, and you will need to iterate over those lines to find the maximum length. The disk access is likely to be the bottleneck of course.

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

Sidebar

Related Questions

I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have a collection of string. I want to create an image out of
I have a collection of IEnumerable<sentence> (sentence = string) I want to split all
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
I have a ViewModel class that looks like this. class MyViewModel : Screen {
Please have a look at the code below: import string from collections import defaultdict
Suppose you have a collection of Foo classes: class Foo { public string Bar;
I have a Person class which has a String collection of aliases representing additional
Let's say we have a collection of Person objects class Person { public string

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.