Hi again,

I have the simple code below, and I receive the error "best overloaded method match has some invalid arguments". I tried to return a string or an int and then it said that I can't implicitly convert void to string. Any ideas on how I can overcome it?

Thanks in advance,

using System;

namespace PrintPattern
{
    class Program
    {
        static void Main()
        {

        }

        static void ReturnPattern(int n)
        {

            if (n == 0)
            {
                return;
            }

            Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1),n,ReturnPattern(n-1)); //error            
        }
    }
}

In line 12 you declare the method as void, meaning there is no return value.

In line 20 you tell the Console.WriteLine to use the return value from ReturnPattern, which as we've previously seen, it has none. This is your error. If you want to use it as part of the WriteLine statement, it must return something (and not be declared void).

I tried to convert it to both int and string but then I got the error "can't implicitly convert void to string/int". What to do?

I tried to convert it to both int and string but then I got the error "can't implicitly convert void to string/int". What to do?

My post tries to clarify on previously given information.

You defined your ReturnPattern method as follows:
static void ReturnPattern(int n).

Then you try to use Console.WriteLine as follows:
Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1), n, ReturnPattern(n-1));

You probably tried fixing it by adding explicit casts in a way like:
Console.WriteLine("{0}{1}{2}", (string) ReturnPattern(n-1), n, (string) ReturnPattern(n-1));

Which results in a compiler error:
can't implicitly convert void to string/int

void means "no value", conversions apply to values, if there's no value, then how can it be converted? Method parameters take values.
You can't pass in something of type void where a value is expected, and void really means that there's no value.
If you want to use your ReturnPattern method inside a Console.WriteLine then you must make sure that a call to ReturnPattern produces a return value, which in turn can be passed as argument into parameters of another method such as Console.WriteLine.

Edited 3 Years Ago by mvmalderen

Sorry, I didn't explain it clearly. What I did was i tried with static int/static string ReturnPattern(...) and tried both with an int value and with a string value - still didn't work. For example, I tried this:

using System;

namespace PrintPattern
{
    class Program
    {
        static void Main()
        {

        }

        static string ReturnPattern(int n)
        {

            if (n == 0)
            {
                return "";
            }

           return Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1),n,ReturnPattern(n-1));

        }
    }
}

This one gives me "Cannot explicitly convert void to string".

Edited 3 Years Ago by cerealBoxx: missing stuff

My bad, the following part of my post is incorrect:

You probably tried fixing it by adding explicit casts in a way like:
Console.WriteLine("{0}{1}{2}", (string) ReturnPattern(n-1), n, (string) ReturnPattern(n-1));

Which results in a compiler error:
can't implicitly convert void to string/int

(explicit casts don't result in an implicit conversion)

This one gives me "Cannot explicitly convert void to string".

The error is in line 20:
return Console.WriteLine("{0}{1}{2}", ReturnPattern(n-1),n,ReturnPattern(n-1));

Console.WriteLine has return type void meaning that there is no value.
But you are trying to use it in a return statement of a method that you have defined to have a return type of string.

Edited 3 Years Ago by mvmalderen

Console.WriteLine returns void so return a string first and then feed that string to the WriteLine method.

This article has been dead for over six months. Start a new discussion instead.