Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
  首页  :: 联系 :: 订阅 订阅  :: 管理

Counting Number Down In Console Application

Posted on 2006-01-14 22:35  Sheva  阅读(329)  评论(0编辑  收藏  举报
    When I first learnt assembly language programming in college, I planned to write a programme which can count down a specified number , and I can still remember how tricky that was when you had to write such a programme in assembly language, but now in C#, you can achieve this just with some trivial code:
using System;

class Program
{
    
static void Main(string[] args)
    {
        
for (Int32 i = 0; i < 50; i++)
        {
            Console.Write(
"Counting {0}\r", i);
            System.Threading.Thread.Sleep(
200);
        }
    }
}

and in C#2.0, you can achieve the same thing using the following code:
using System;

class Program
{
    Int32 currentPos 
= Console.CursorTop;
    Console.CursorVisible 
= false// Hide the cursor, so you don't see the flickering console output
    static void Main(string[] args)
    {
        
for (Int32 i = 0; i < 50; i++)
        {
            Console.WriteLine(
"Counting {0}", i);
            Console.CursorTop 
= currentPos;
            System.Threading.Thread.Sleep(
200);
        }
    }
}