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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:13:18+00:00 2026-06-11T19:13:18+00:00

The following is a short clock program from the book programming in the key

  • 0

The following is a short clock program from the book “programming in the key of c#”. I’m not familiar with the Timers library at all so some of this syntax I don’t really get. What I want to understand and I don’t is the line Console.Write(str) in the method in this little program. How does Main know what to print to the console? Is it the empty Console.WriteLine() call that makes the time print out every second? When I’m reading about these concepts it seems easy after the fact to understand what’s going on. Based on what I’ve asked, what are the things about C# that I don’t really understand yet?

using System;
using System.Timers;    // Requires System.dll

class Clock
{
    static int iStringLength;

    static void Main()
    {
        Console.WriteLine("Press Enter to end program");
        Console.WriteLine();

        Timer tmr = new Timer();
        tmr.Elapsed += new ElapsedEventHandler(TimerHandler);
        tmr.Interval = 1000;
        tmr.Start();

        Console.ReadLine();
        tmr.Stop();
    }
    static void TimerHandler(object obj, ElapsedEventArgs eea)
    {
        Console.Write(new String('\b', iStringLength));

        string str = String.Format("{0} {1} ", 
                                eea.SignalTime.ToLongDateString(), 
                                eea.SignalTime.ToLongTimeString());
        iStringLength = str.Length;

        Console.Write(str);
    }
}
  • 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-11T19:13:19+00:00Added an answer on June 11, 2026 at 7:13 pm

    str contains the value of the string after the String.Format() function is called. That function is documented here: http://msdn.microsoft.com/en-us/library/b1csw23d.aspx

    In your code, the {0} is replaced by the formatted representation of eea.SignalTime.ToLongDateString(), and the {1} is replaced by a formatted representation of eea.SignalTime.ToLongTimeString().

    So to answer

    What I want to understand and I don’t is the line Console.Write(str)
    in the method in this little program. How does Main know what to print
    to the console?

    the answer is “It writes whatever the String.Format() function has determined the value of “str” is in this line:”

    string str = String.Format("{0} {1} ",  
                                    eea.SignalTime.ToLongDateString(),  
                                    eea.SignalTime.ToLongTimeString()); 
    

    The WriteLine() function just prints an empty line and really has nothing to do with the string that shows the date/time as you asked.

    For the record, Console.Write and Console.Writeline are documented here and here, respectively.

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

Sidebar

Related Questions

I'm using the following short program to test std::clock() : #include <ctime> #include <iostream>
The following short Fortran90 program crashes as long as it contains the MPI_GET call.
Consider the following short program: #include <type_traits> #include <iostream> using namespace std; template <typename
In short, currently I am using the following code to pull records from multiple
I'm using the following query to extract the frequent short values from a mediumblob
I have the following short version of a tsql if else if.. IF @var
I have been following a short tutorial to build a tab menu on my
Following problem: I want to render a news stream of short messages based on
Short question, is the following ok: struct X { A& x; A y; X()
In the short version of postgres' installing it tell me to do the following

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.