Lesson 3.4 The Return Keyword
A note worth making is that of the return keyword. It is a simple word that has a couple of good uses. It is also a source of consternation between different schools of coding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Return1
{
class Program
{
static void Main(string[] args)
{
WriteText();
System.Console.ReadKey(true);
}
static void WriteText()
{
System.Console.Write("This text will get shown...");
return;
System.Console.Write("...this text will not");
}
}
}
This simple example shows how a return statement can be used to bail out of the execution of a method.

The program executes the WriteText method:
- it writes the first line of text
- it hits the return and stops the WriteText method immediately
- It goes back to where it was called from.
static void WriteText()
{
System.Console.Write("This text will get shown...");
return;
System.Console.Write("...this text will not");
}
That line of code that writes the extra text in WriteText will never be gotten to. In fact, it is quite common for Visual Studio to give you a warning by underlining a bit of your code in a green wavy line. Hovering over it will give you a message “Unreachable code detected.” It is just that, unreachable.
This brings us to a pedantry point of fact that wars have been fought over. Ok, not quite wars but it has seen many coders wade in to give their penny’s worth. This is mostly concerned with functions. Should you be able to put a return statement anywhere in the function or only at the end? I appreciate this may seem a strange thing to quarrel about. There are arguments both for and against. First, let’s just illustrate what I’m talking about.
Below is a program that is quite pointless except for showing my point. It has a function in it called GetNumberString; it takes an integer value between 0 and 9 and returns the word for that number.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Return2
{
class Program
{
static void Main(string[] args)
{
string numberString = GetNumberString(1);
System.Console.Write(numberString);
System.Console.ReadKey(true);
}
static string GetNumberString(int number)
{
switch (number)
{
case 0: return "zero";
case 1: return "one";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
default: return "Cannot translate numbers that aren't 0-9";
}
}
}
}
There are a couple of things to make note of regarding the GetNumberString function. Firstly, it’s terrible; I wouldn’t write a professional piece like this to accomplish this task. Secondly, on a side note, there are no break statements in the switch block. This is because you don’t need one if you have a return statement at the end of a case block.
The third point to note is that I have no less than 10 return statements. This is perfectly legal and C# will run it just fine. However, a more purist way of coding for a lot of people would have been to write the method in this way.
static string GetNumberString(int number)
{
string toReturn;
switch (number)
{
case 0: toReturn = "zero";
break;
case 1: toReturn = "one";
break;
case 2: toReturn = "two";
break;
case 3: toReturn = "three";
break;
case 4: toReturn = "four";
break;
case 5: toReturn = "five";
break;
case 6: toReturn = "six";
break;
case 7: toReturn = "seven";
break;
case 8: toReturn = "eight";
break;
case 9: toReturn = "nine";
break;
default: toReturn = "Cannot translate numbers that aren't 0-9";
break;
}
return toReturn;
}
Now there is only one return statement. This is more of a purist’s approach.
The arguments mainly centre round readability. One school of thought claims that having one “exit” point from the function means less confusion. The other school, to which I belong, claim that sticking too rigidly to having one exit point can make code contrived and unnecessarily complicated.
I’ll let you make your own mind up on this. If you work for a software house they may or may not have a rule about this. I don’t consider important enough to get your knickers in a twist over. No doubt I would be derided and then burned for heresy in certain circles.