How to stop void method (return in void method)

This is quick blog post about stopping executing void method. So, how to stop executing void return type method? We just need to use ‘return’ statement itself, for example:

public static void sum(int a, int b)
{
    if (a == 0) return;
    Console.WriteLine(a+b);
}

If we pass 0 as first argument then we won’t get print from next line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: