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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:17:19+00:00 2026-06-19T01:17:19+00:00

This is purely to simplify the code and make it more readable. I print

  • 0

This is purely to simplify the code and make it more readable.

I print out a list of Windows Services on my computer. In doing so, I call the ServiceController.GetServices() and retrieve an array of the services. But I would prefer to get it in a strongly typed list and print it out to my txbMain text box. I have managed to both of these things in 3 lines of code. But is there a way to print out to txbMain in the first line so I can avoid the foreach loop?

List<ServiceController> list = ServiceController.GetServices()
    .OrderBy(x => x.DisplayName)
    .ToList();

foreach (ServiceController sc in list)
    txbMain.Text += sc.DisplayName + Environment.NewLine;
  • 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-06-19T01:17:20+00:00Added an answer on June 19, 2026 at 1:17 am

    If you don’t use your list after this statement, you can do :

    ServiceController.GetServices()
        .OrderBy(x => x.DisplayName)
        .ToList()
        .ForEach(x => txbMain.Text += x.DisplayName + Environment.NewLine);
    

    Note that it will internally perform a foreach loop anyway.

    Worse, it will first have to create a list before using the ForEach method, which is time consuming.

    See Eric Lippert’s post about it.

    Maybe you should stick with your foreach loop.

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

Sidebar

Related Questions

Here is what I am doing, this is purely through PuTTy on my windows
System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself
This is purely a theory question, so, given an "infinite" time to make a
This is purely out of curiosity, but why does this occur? >>> a =
This is purely a curiosity item -- the solution turned out to be to
Possible Duplicate: Why does Double.NaN==Double.NaN return false? This is purely out of curiosity. I
I am interested in doing this purely using bash.
This is purely for learning more about output buffering and nothing more. What I
This is purely hypothetical, but I'm not sure whether the following code will result
This is purely theoretical question, I'm not doing anything, but I plan to. Is

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.