Results 1 to 6 of 6

Thread: [2.0] How to do a clean exit from console application.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    [2.0] How to do a clean exit from console application.

    Based on the result of my checking, I would like to exit out from the console application decently without throwing any exceptions.

    static void Main(string[] args)
    {

    //Check to see if today is a US Holiday.
    If(Holiday.IsUSHoliday(DateTime.Today));
    //close the program and how to do that?

    }

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] How to do a clean exit from console application.

    Try:

    return;

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] How to do a clean exit from console application.

    Quote Originally Posted by mendhak
    Try:

    return;
    it works in C, C++, Java and I bit it'd work in C#
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] How to do a clean exit from console application.

    Using the return keyword anywhere except for the last line of a procedure is spaghetti code. You should structure your if{} blocks in a manner such that if the condition arises where the application should exit, the code path naturally jumps to the end of the Main() procedure.

    For example:
    Code:
    // Bad:
    static void Main(string[] args)
    {
      if (Holiday.IsUSHoliday(DateTime.Today)) {
        return;
      }
    
      // rest of code
    }
    
    
    // Good:
    static void Main(string[] args)
    {
      if (!Holiday.IsUSHoliday(DateTime.Today)) {
        // rest of code
      }
    }

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] How to do a clean exit from console application.

    Quote Originally Posted by penagate
    Using the return keyword anywhere except for the last line of a procedure is spaghetti code.
    Does this mean that Italians aren't very good coders?

  6. #6
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [2.0] How to do a clean exit from console application.

    you could just use
    Code:
    Environment.Exit(0);
    when you want to exit from the program. more info on msdn.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width