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

The Archive Base Latest Questions

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

I have a piece of C# code in which I use the goto statement.

  • 0

I have a piece of C# code in which I use the goto statement. Is this a correct use of the goto statement or is there a better alternative solution?

bool IsValid(TestObject aObject)
{
   bool aRetVal = false;

   if(condition here)
       goto exit;
   if(condition here)
       goto exit;
   if(condition here)
       goto exit;

   aRetVal = true;
   exit:
   return aRetVal;
}

The reason I’m doing this is because I don’t want multiple exit points in my method.

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

    No – use return instead. Why force someone reading your code to skip to an exit point and then return? You know everything you need to do at this point – so the clearest solution is to return, IMO.

    The “don’t have multiple exit points” idea was appropriate in languages where you’d need to do things like cleanup on the exit of a function, but between garbage collection and finally blocks, it’s pointless and counterproductive in C#.

    What do you want to do if the condition is met? Return from the method. So make your code say that. Wherever you can make the code say exactly what you mean, that’s a good thing. Don’t make it more complicated than it needs to be.

    I’m assuming your real situation is more complicated than just these conditions, otherwise I’d use something like Marcelo’s answer, but probably written as:

    return !(condition1 || condition2 || condition3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code which I'm hoping will be able to tell
I am somewhat new to R, and i have this piece of code which
I have piece of code which I use to format a range of cells
I have this piece of code which works 90% of the time: $user_details=$fb->api_client->users_getInfo($fb_user, array('last_name','first_name','proxied_email'));
I have a piece of code which looks like this: Snippet A: class Creature
I have a problem with this piece of code If I don't use the
I have this piece of code which i am using on another area of
I have this piece of code which gives to SAXParseError i.e., it is not
I have this piece of code which checks whether a given number is prime:
I have a piece of code below which I am trying to use to

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.