C# Exquisite Corpse Exercise

Hi, I’ve been working on the Exquisite Corpse Exercise in C# and have found an error I’m stuck on and unsure on how to resolve. I keep receiving “error CS0161: ‘Program.BuildACreature(string, string, string)’: not all code paths return a value” now I somewhat understand the meaning behind this error, but after trying to watch through the video connected to this exercise I’m somewhat lost.

using System; namespace ExquisiteCorpse { class Program { static void Main(string[] args) { BuildACreature("bug", "bug", "bug"); } static string BuildACreature(string head, string body, string feet) { // Head switch (head) { case "ghost": GhostHead(); break; case "bug": BugHead(); break; case "monster": MonsterHead(); break; } // Body switch (body) { case "ghost": GhostBody(); break; case "bug": BugBody(); break; case "monster": MonsterBody(); break; } // Feet switch (feet) { case "ghost": GhostFeet(); break; case "bug": BugFeet(); break; case "monster": MonsterFeet(); break; } } 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(" \"\"\"\" \"\"\"\""); } } }

I’ve made sure I haven’t made any typos and that the Methods being called in the switch’s aren’t misspelled either. I don’t understand how the error still appears. I’ve basically written the youtube video’s code word for word. Please help.

Hello @byte2823472559, welcome to the forums! When you define a method/function like this:

public int someFunc(string someParam){ return 5; }

The int after the public keyword determines the return type of the function. That means, for the above function, it has to return an integer. If it doesn’t, it will throw an error.


Note, all pathways must lead to a return, so if you have chained else if blocks, each one must either return something, or allow for a value to be returned:

public int someFunc(string someParam){
if (someParam == "Hello") {
  return 5;
} else if (someParam == "yes"){
  return 3;
} else {
   Console.WriteLine("Not yes or Hello");
 }
}
//that function would throw an error, as in the else block, nothing is returned
//the below function wouldn't throw an error:
public int someFunc(string someParam){
if (someParam == "Hello") {
  return 5;
} else if (someParam == "yes"){
  return 3;
} else {
   Console.WriteLine("Not yes or Hello");
   return 0;
 }
}

I hope this helps!