Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Can I use goto instruct to transfer to specific function or event?
Posted
Updated 14-Mar-10 13:46pm
v4

goto is basically useless. It makes your code unreadable. I've never used it once in 10 years as a professional developer.
 
Share this answer
 
After having a look at the MSDN documentation, at first shot, I would say "No. I cannot."
See here.

"In the first form, the identifier indicates a label located in the current body, the same lexical scope, or an enclosing scope of the goto statement.

A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.

The goto statement is also useful to get out of deeply nested loops."

The first point says that the goto statement can be used within the same lexical scope, so that means it cannot be made to point to another method. The same thing should apply to an event.
 
Share this answer
 
v2
mohammed sadeq wrote:
Can I use goto instruct to transfer to specific function or event?


Absolutely not.

Think about it: you have a method (called returnsPoint), returning a Point. Inside this method, you use a goto to jump into a method returning a bool (called returnsBool). If you execute Point loc = returnsPoint(); what kind of value ends up in "loc"?

If you have to ask the question, then do not use "goto" under any circumstances. In a few years, when you have real-world experience under your belt, and can understand why they seriously considered not including goto in C# at all, then you will understand when it can be used. For now, just pretend it doesn't exist - almost nothing you code in the early days will be in any way improved by its use!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900