-2

I'm trying to build a square shape

string BuildSquare(int n)
{
    for (int p = 1; p <= n; p++)
    {
        for (int i = 1; i <= n; i++)
        {
            Console.Write("+");
        }
        Console.WriteLine();
    }
}

When done, the above error keeps showing not all code paths return a value I've tried returning 0, null, true; but they all fail

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
joshodey
  • 9
  • 1
  • 2
    `string BuildSquare(int n)` is defined as returning a string. no code path returns anything at all ever. – Ňɏssa Pøngjǣrdenlarp Sep 19 '22 at 23:54
  • Does this answer your question? [C# compiler error: "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-compiler-error-not-all-code-paths-return-a-value) – Heretic Monkey Sep 20 '22 at 00:04
  • There are quite a few questions regarding this error on Stack Overflow and indeed online. Please, do some research before asking. – Heretic Monkey Sep 20 '22 at 00:05

1 Answers1

2

BuildSquare must return a string. Your code is not returning anything.

Console.Write/Console.WriteLine is not a way to return anything.

Based on your code I think you need this:

string BuildSquare(int n)
{
    var sb = new StringBuilder();
    for (int p = 1; p <= n; p++)
    {
        for (int i = 1; i <= n; i++)
        {
            sb.Append("+");
        }
        sb.AppendLine();
    }
    return sb.ToString();
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172