Dave Dave - 3 months ago 8
C# Question

Displaying user name with the help of a method in C#

Cant't figure out what I am doing wrong,want to display user name in console through a method in the same class,missing a minor syntax please have a look and guide me.

public class Program
{
string name;
public void GetName()
{
Console.WriteLine("Enter ur name");
name = Console.ReadLine();
Console.WriteLine("Name is",name);
Console.ReadLine();

}

static void Main()
{
Program p = new Program();
p.GetName();
Console.ReadLine();
}

}

Answer

You need to specify the format string try:

public class Program
  {
   string name;
   public void GetName()
   {
       Console.WriteLine("Enter ur name");
       name = Console.ReadLine();
       Console.WriteLine("Name is {0}",name);
       Console.ReadLine();

   }

    static void Main()
    {
        Program p = new Program();
        p.GetName();
        Console.ReadLine();
    }

}