I am new to C# and I'm trying to get a basic I/O program to work asking for the users name and age. The program doesn't allow me to run because the "static void GetUserData()" part, giving this error:
Expected class, delegate, enum, interface, or struct.
It does not like the void keyword.
Also the method GetUserData() gives the error Does not exist in current context. As far as I know this follows the rules in C#, I am declared a method to use later, and the declaration is made within a class so it should be ok?
Thanks all I got it sorted. I typed the program exactly how the book said, its a lesson learned anyway.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BasicConsoleIO
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("basic input output");
GetUserData();
Console.ReadLine();
}
}
static void GetUserData()
{
Console.Write("please enter your name: ");
string userName = Console.ReadLine();
Console.Write("please enter your age: ");
string userAge = Console.ReadLine();
//changes echo colour
ConsoleColor prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.red;
//echo to console
Console.WriteLine("hello {0}! Your are {1} years old.", userName, userAge);
//Restore previous color
Console.ForegroundColor = prevColor;
}
}