If I write a for, do, or while loop, is it possible to come out of this with the return keyword?
Eg:
class BreakTest
{
public static void Main()
{
You can do this. Calling return;
will exit Main(). Normally you'd use break;
to break out of the for loop instead.
That being said, it's often considered bad practice to have multiple return locations within a single function. Many developers prefer to have a single return statement (if the method isn't declared with void
). This can make the code more easy to follow, from an understandability point of view, and hence easier to maintain and test.