0

I am New to C# and I'm having some trouble with Console.WriteLine(); Method.

using System;

namespace ConsoleApp
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a String");
            string passData = Console.ReadLine();
            printer(passData);
        }
      
        public static string printer(string data)
        {
            Console.WriteLine(data);
        }
    }
}

I could not use the Method inside a function. I am Getting an error as shown below.

Severity Code Description Project File Line Suppression State
Error CS0161 'Program.printer(string)': not all code paths return a value ConsoleApp     
C:\Users\ravik\source\repos\ConsoleApp\ConsoleApp\Program.cs    14  Active

Any Explanation would be appreciated! Thanks.

Jonas W
  • 3,200
  • 1
  • 31
  • 44
Ravikiran
  • 512
  • 3
  • 14
  • 2
    Your methods' return type is `string`. It lacks the return statement, if you are not going to return anything, then make it `void`. – SᴇM Mar 17 '21 at 06:00
  • One of the first thing to learn when learning to code: read and _understand_ your error messages. This has clearly nothing to do with `Console.WriteLine`. As the error message tells you, the compiler expects you to _return_ something, because the method is of return type `string`. – René Vogt Mar 17 '21 at 06:03
  • A good thing to do if you expect a piece of code to be the problem, but you're not sure why, is to remove it entirely for a moment and see what happens. If you remove the `Console.WriteLine`, you'd see the message doesn't change and it's not relevant to the problem. – Jeroen Mostert Mar 17 '21 at 06:03

1 Answers1

1

Your method should look like:

   public static void printer(string data)
   {
       Console.WriteLine(data);
   }

or if you want to return a string then:

   public static string printer(string data)
   {
       Console.WriteLine(data);
       return "printed";
   }
josibu
  • 594
  • 1
  • 6
  • 23