I decided to split up the code into two files to illustrate how to call a separate class. It is overkill for such a simple application, but it is an example!
[ Program.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloUser
{
class Program
{
public static void GetUserInput()
{
string strInput;
Console.Write("Enter your first name: ");
strInput = Console.ReadLine();
Console.WriteLine("Hello " + strInput);
Console.ReadLine();
}
public static void SetGreeting()
{
string strTitle = "Hello User 1.2";
Console.Title = strTitle;
}
}
}
[ AppTest.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloUser
{
class AppTest
{
static void Main(string[] args)
{
Program.GetUserInput();
Program.SetGreeting();
}
}
}
Visual c# 2008 Hello User 1.2 Console App...
Hello User Java Edition Console App...
import java.util.*;
public class HelloUser
{
public static void main(String[] args)
{
GetUserInput();
}
private static void GetUserInput()
{
String strInput;
Scanner in = new Scanner(System.in);
System.out.println("Enter your first name: ");
strInput = in.nextLine();
in.close();
System.out.println("Hello " + strInput);
}
}
Visual c# 2008 Hello User 1.1 Console App...
This is a much better approach of writing Hello User. It does require a few more lines of code, but it is better for modularity purposes. It is not a good practice to write everything in the Main method as I did in the previous examples.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloUser
{
class Program
{
static void Main(string[] args)
{
SetConsoleTitle();
GetUserInput();
}
static void GetUserInput()
{
string strInput;
Console.Write("Enter your first name: ");
strInput = Console.ReadLine();
Console.WriteLine("Hello " + strInput);
Console.ReadLine();
}
static void SetConsoleTitle()
{
string strTitle = "Hello User 1.1";
Console.Title = strTitle;
}
}
}
Prompt for input
Write to screen
Visual c# 2008 Hello User Console App...
The HelloWorld app showed you how to make a simple app that uses static data. Here is an app that prompts for user input. Same concept just a few more lines!
// Directive
using System;
using System.Collections.Generic;
using System.Linq; // Remove if being used in Visual C# 2005
using System.Text;
namespace HelloUser
{
class Program
{
// Main Method
static void Main(string[] args)
{
// Data Members
string strTitle = "Hello User 1.0"; // Set string value
string strInput; // Declare variable
Console.Title = strTitle; // Set console title
Console.Write("Enter your first name: "); // Prompt for input
strInput = Console.ReadLine(); // Read user input
Console.WriteLine("Hello " + strInput); // Write line of text
Console.ReadLine(); // Read line of text
}
}
}
Visual c# 2008 Hello World Console App...
I figured I would post a simple Hello World Console Application built with Visual C# 2008. So here we go.
// Directive
using System;
using System.Collections.Generic;
using System.Linq; // Remove if being used in Visual C# 2005
using System.Text;
namespace HelloWorld
{
class Program
{
// Main Method
static void Main(string[] args)
{
// Data Members
string strOutput = "Hello World"; // Set string value
Console.Title = strOutput; // Set console title
Console.WriteLine(strOutput); // Write line of text
Console.ReadLine(); // Read line of text
}
}
}