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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:13:03+00:00 2026-05-17T16:13:03+00:00

Usually we do something like a for or while loop with a counter: for

  • 0

Usually we do something like a for or while loop with a counter:

for (int i = 0; i < 10; i++)
{
    list.Add(GetRandomItem());
}

but sometimes you mix up with boundaries. You could use a while loop instead, but if you make a mistake this loop is infinite…

In Perl for example I would use the more obvious

for(1..10){
    list->add(getRandomItem());
}

Is there something like doitXtimes(10){...}?

  • 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-17T16:13:04+00:00Added an answer on May 17, 2026 at 4:13 pm

    Well you can easily write your own extension method:

    public static void Times(this int count, Action action)
    {
        for (int i = 0; i < count; i++)
        {
            action();
        }
    }
    

    Then you can write:

    10.Times(() => list.Add(GetRandomItem()));
    

    I’m not sure I’d actually suggest that you do that, but it’s an option. I don’t believe there’s anything like that in the framework, although you can use Enumerable.Range or Enumerable.Repeat to create a lazy sequence of an appropriate length, which can be useful in some situations.


    As of C# 6, you can still access a static method conveniently without creating an extension method, using a using static directive to import it. For example:

    // Normally in a namespace, of course.
    public class LoopUtilities
    {
        public static void Repeat(int count, Action action)
        {
            for (int i = 0; i < count; i++)
            {
                action();
            }
        }
    }
    

    Then when you want to use it:

    using static LoopUtilities;
    
    // Class declaration etc, then:
    Repeat(5, () => Console.WriteLine("Hello."));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually do something like the example below when I need to return error
Doesn't an ORM usually involve doing something like a select *? If I have
Usually when I need to fork in C, I do something like this: pid_t
When I am unsure about some thing for a project, I usually use a
Usually when I'm creating indexes on tables, I generally guess what the Fill Factor
Usually Flash and Flex applications are embedded on in HTML using either a combination
Usually when I build a site, I put all the CSS into one file,
Usually, we install VS.NET on our production server, to solve problems easily with our
Usually when I plot in MATLAB , it always draws on the same figure.
Usually, when using Windows Authentication, the software (eg Internet Explorer) tries to use Windows

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.