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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:41+00:00 2026-05-23T11:49:41+00:00

Context: .NET, C#, but the question is about OOP in general. When I write

  • 0

Context: .NET, C#, but the question is about OOP in general.

When I write a class that should act as a “service”, like a socket listener, or a timer, I see two approaches when it comes to coding it:

  1. Create a constructor, and inside the constructor, immediately start the background task. For instance:

    public class MyTimer
    {
        private readonly TimeSpan interval;
    
        public MyTimer(TimeSpan interval)
        {
            this.interval = interval;
            StartTicking();
        }
    
        private void StartTicking()
        {
            // do the ticking logic
        }
    }
    
  2. Create a constructor that accepts the class’ settings, and add an explicit method for starting up:

    public class MyTimer
    {
        private readonly TimeSpan interval;
    
        public MyTimer(TimeSpan interval)
        {
            this.interval = interval;
        }
    
        public void StartTicking()
        {
            // do the ticking logic
        }
    }
    

I tend to think that the second approach is better:

A. The constructor is used only for creating a valid instance, keeping it minimal and clean.

B. The developer who actually uses my class is less astonished.

C. The hardware resources are not overused, since the “service” class does not immediately use them.

What do you think? Is it only a matter of coding style, or is it more than that?

  • 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-23T11:49:42+00:00Added an answer on May 23, 2026 at 11:49 am

    Don’t start running in your constructor.

    • Users of your API won’t expect that, and it makes your class harder to use
    • From an exception handling standpoint, you want to be able to report an error that happens when constructing an object, separately from an error that happens during execution.
    • It prevents sharing instances of your object, if you ever wanted to do something like a static factory singleton pattern.
    • I would second StriplingWarrior’s point that there are many good reasons, like dependency injection, where object creation needs to happen first so that some other class can run it later.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That may seem like a broad question, but I'm talking specifically here about apps
Context: .NET 3.5, VS2008. I'm not sure about the title of this question, so
Sorry for yet another question about EF4 context lifespan, but I've been wondering about
Recently I've started learning about Contexts in .NET (context-bound, context-agile, message sinks, etc.). Several
Context: I'm in charge of running a service written in .NET. Proprietary application. It
I've asked questions about Quartz.NET a lot today, but I'm making progress in understand
Important: This question isn't actually really an ASP.NET question. Anyone who knows anything about
This is a somewhat subjective question, but I'd like to hear the pros/cons for
I have a question about how to keep the current ASP.net/User's session during the
I have a question about the login control of asp.net. I defined on my

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.