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

  • Home
  • SEARCH
  • 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 949095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:21:03+00:00 2026-05-15T23:21:03+00:00

I am still pretty new to C# and am having a difficult time getting

  • 0

I am still pretty new to C# and am having a difficult time getting used to it compared to C/CPP.

How do you exit a function on C# without exiting the program like this function would?

if (textBox1.Text == "" || textBox1.Text == String.Empty || textBox1.TextLength == 0)
    textBox3.Text += "[-] Listbox is Empty!!!!\r\n";
System.Environment.Exit(0);

This will not allow return types and if left alone it will keep going on through the function unstopped. Which is undesirable.

  • 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-15T23:21:03+00:00Added an answer on May 15, 2026 at 11:21 pm

    There are two ways to exit a method early (without quitting the program):

    • Use the return keyword.
    • Throw an exception.

    Exceptions should only be used for exceptional circumstances – when the method cannot continue and it cannot return a reasonable value that would make sense to the caller. Usually though you should just return when you are done.

    If your method returns void then you can write return without a value:

    return;
    

    Specifically about your code:

    • There is no need to write the same test three times. All those conditions are equivalent.
    • You should also use curly braces when you write an if statement so that it is clear which statements are inside the body of the if statement:

      if (textBox1.Text == String.Empty)
      {
          textBox3.Text += "[-] Listbox is Empty!!!!\r\n";
      }
      return; // Are you sure you want the return to be here??
      
    • If you are using .NET 4 there is a useful method that depending on your requirements you might want to consider using here: String.IsNullOrWhitespace.

    • You might want to use Environment.Newline instead of "\r\n".
    • You might want to consider another way to display invalid input other than writing messages to a text box.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. I'm still pretty new to iPhone development, and I'm having a bit
I'm still pretty new to JSON and GWT and I'm trying to figure out
Still new to Objective C, and I'm having some trouble that I just can't
I'm still pretty new to Objective -C and iPhone development and am trying to
Sorry for the semi-generic title, but I'm still pretty new at rails and couldn't
I'm pretty green still when it comes to web programming, I've spent most of
still new to the world of linq, and i need some help flatening a
I still very new using Subversion. Is it possible to have a working copy
I'm having a problem echoing a single line from a sql query. I'm still
Still 'diving in' to Python, and want to make sure I'm not overlooking something.

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.