Why am i getting an unreachable code error in Exquisite Corpse?

Hello,
Can anyone please also tell me why my code runs as wanted despite this error? I thought code with errors will not run???
Thank you :slight_smile:

** Error Messages **
Program.cs(69,9): warning CS0162: Unreachable code detected [/home/ccuser/workspace/csharp-exquisite-corpse/workspace.csproj]
Program.cs(72,9): warning CS0162: Unreachable code detected [/home/ccuser/workspace/csharp-exquisite-corpse/workspace.csproj]
Program.cs(77,9): warning CS0162: Unreachable code detected [/home/ccuser/workspace/csharp-exquisite-corpse/workspace.csproj]
…-…
( o o )
| O |
–| | |–
–| | |–
–| | |–
| |
| |
‘~
…-…
( o o )
| O |
| |
| |
| |
| |
| |
'
~’
$

**Code **

using System;

namespace ExquisiteCorpse
{
  class Program
  {
    static void Main(string[] args)
    {
      BuildACreature("ghost", "bug", "ghost");
     // SwitchCase(1,1,1);
      RandomMode();
    }
   
    static void RandomMode(){
      Random randomNumber = new Random();
      int head = randomNumber.Next(1, 4); 
      int body = randomNumber.Next(1, 4);
      int feet = randomNumber.Next(1, 4);
      SwitchCase(head, body, feet);
     }
    static void SwitchCase(int head, int body, int feet){
      switch (head){
        case 1:
        GhostHead();
        break;
        case 2:
        BugHead();
        break;
        case 3:
        MonsterHead();
        break;
        default: 
        Console.WriteLine("An error has occured with the head switch statement");
        break;
      }
       switch (body){
        case 1:
        GhostBody();
        break;
        case 2:
        BugBody();
        break;
        case 3:
        MonsterBody();
        break;
        default: 
        Console.WriteLine("An error has occured with the body switch statement");
        break;
      }
       switch (feet){
        case 1:
        GhostFeet();
        break;
        case 2:
        BugFeet();
        break;
        case 3:
        MonsterFeet();
        break;
        default: 
        Console.WriteLine("An error has occured with the head switch statement");
        break;
      }
    }
    static int TranslateToNumber(string creature){
      switch (creature){
        case "ghost":
        return 1; 
        break;
        case "bug" :
        return 2; 
        break; 
        case "monster" :
        return 3; 
        default: 
        return 1; 
        break;
      }
    }
     static void BuildACreature (string head, string body, string feet){
      int headNum = TranslateToNumber(head);
      int bodyNum = TranslateToNumber(body);
      int feetNum = TranslateToNumber(feet);
      SwitchCase(headNum, bodyNum, feetNum);
    }


    static void GhostHead()
    {
      Console.WriteLine("     ..-..");
      Console.WriteLine("    ( o o )");
      Console.WriteLine("    |  O  |");
    }

    static void GhostBody()
    {
      Console.WriteLine("    |     |");
      Console.WriteLine("    |     |");
      Console.WriteLine("    |     |");
    }

    static void GhostFeet()
    {
      Console.WriteLine("    |     |");
      Console.WriteLine("    |     |");
      Console.WriteLine("    '~~~~~'");
    }

    static void BugHead()
    {
      Console.WriteLine("     /   \\");
      Console.WriteLine("     \\. ./");
      Console.WriteLine("    (o + o)");
    }

    static void BugBody()
    {
      Console.WriteLine("  --|  |  |--");
      Console.WriteLine("  --|  |  |--");
      Console.WriteLine("  --|  |  |--");
    }

    static void BugFeet()
    {
      Console.WriteLine("     v   v");
      Console.WriteLine("     *****");
    }

    static void MonsterHead()
    {
      Console.WriteLine("     _____");
      Console.WriteLine(" .-,;='';_),-.");
      Console.WriteLine("  \\_\\(),()/_/");
      Console.WriteLine("   (,___,)");
    }

    static void MonsterBody()
    {
      Console.WriteLine("   ,-/`~`\\-,___");
      Console.WriteLine("  / /).:.('--._)");
      Console.WriteLine(" {_[ (_,_)");
    }

    static void MonsterFeet()
    {
      Console.WriteLine("    |  Y  |");
      Console.WriteLine("   /   |   \\");
      Console.WriteLine("   \"\"\"\" \"\"\"\"");
    }
  }
}

Consider what return does.

1 Like

Hi
in addition to the reply from midlindner , ‘unreachable code’ is a warning not an error.
It’s saying ‘you’ve got code here that’ll never be used. Is that intentional ?’
So, while there may not be any errors in the code, depending on what’s in the unreachable part, it may not work as expected.

1 Like

Thank you very much! :slight_smile:

1 Like

Thank you for your response! IThis is not an error I have come across before with other languages but it makes sense now.

1 Like