A Simple C# Program
Here's Your first code, it will ask your name and display it in console. Let's take a look to the code:
When you compile and run the code, it will show:
using System; namespace Program1 { public class Program { // This is where your program starts. static void Main(string[] args) { // Prompt user to enter a name. Console.WriteLine(“Enter your name, please:”); // Now read the name entered. string name = Console.ReadLine(); // Greet the user with the name that was entered. Console.WriteLine(“Hello, “ + name); // Wait for user to acknowledge the results. Console.WriteLine(“Press Enter to terminate...”); Console.Read(); } } }
When you compile and run the code, it will show:
Enter your name, please:
Yang Sopiana
Hello, Yang Sopiana
Press Enter to terminate...
Yang Sopiana
Hello, Yang Sopiana
Press Enter to terminate...
More About Simple Program
To compile the program, you can use Visual Studio or the command-line compiler. To use the command-line compiler, in its simplest form,
use the following command in a command window:
Now lets see how your code works:
http://www.microsoft.com/express/download/
You can also download MSDN Library that contains useful help files in case you need.
csc SimpleProgram.cs
Now lets see how your code works:
line 1: | using directive was used to make the System namespace available to our application, so we can use the Console class |
line 2: |
namespace Program1 It creates a new namespace called Program1, namespace is a set of type declarations
associated with a name.
|
line 4: | public class Program This is our program class name. |
line 6: | It's a comment, compiler will ignore all character after comments tag ('//') |
line 7: | static void Main is our main method. A program should have main method, where the application execution starts from. |
line 10: | Console.WriteLine tell system to print some character inside the function. |
line 13: |
string name = Console.ReadLine(); tell application to provide some memory (called variable) to store console
input from user.
|
line 16: | Console.WriteLine("Hello, " + name); the console will write "Hello, " and display 'name' variable. |
Where can I get C# Compiler
Visual C# Express Edition has all the features you’ll need for the examples in this tutorial, and it has the additional advantage of being completely free from Microsoft. Getting C# Express is very simple, just go link below and download Visual C# Express:http://www.microsoft.com/express/download/
You can also download MSDN Library that contains useful help files in case you need.
0 comments:
Post a Comment