Search code examples
c#.netmethodsconsole.writeline

Unable to use Console.WriteLine(); from a method


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.


Solution

  • 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";
       }