We will create the famous "Hello World!" example in a C# console application.
dotnet new console -n HelloWorld
HelloWorld with the necessary files.New Solution in the welcome screen.Console Application under.NET Core.HelloWorld.Create.
Now we're ready to write some code. The project should have a file calledProgram.cs. Open that file to edit the code that will execute.
usingSystem;namespaceConsoleApplication1{classProgram{staticvoidMain(string[]args){}}}
Note that you don't need thestring[] args part of your loop. In fact, you can omit it like so:
usingSystem;namespaceConsoleApplication1{classProgram{staticvoidMain(){}}}
Note: In the end of your C# program, for VSCode or Visual Studio, you need to addConsole.ReadKey(); or else the program will close after executing the last line of code in thestatic void Main() orstatic void Main(string[] args) loop.
usingSystem;
Ensures that the program will use the System namespace. The System namespace includes the console class, which is necessary for input/output operations with the console. It is important to note that each line of code terminates with a semi-colon (unless an open curly bracket is used).
namespaceConsoleApplication1
Places the following code into the ConsoleApplication1 namespace. Later on the programmer may choose to create a namespace with their name or company name. It is important that this namespace name be unique so that it does not conflict with other namespaces when unintended.
{An open curly bracket signifies a block of code. All the code that falls between { and its corresponding } will be affected by the line of code prior to the opening bracket. In this case, these two brackets ensure that all the rest of the code falls within the namespace ConsoleApplication1.
staticvoidMain(string[]args)
staticvoidMain()
The main entry point into the program. When a console application runs it looks for the method entitled Main(string[] args). After the method is found the program will begin to execute at the first line after the Main entry point.
The following code outputs a line of text to the console.
Console.WriteLine("Hello World!");
This code needs to be inserted just after the entry point of the program.
...staticvoidMain(string[]args){Console.WriteLine("Hello World!");}
The code can be compiled and run by pressing F5 or by Debug->Start Debugging. The code will output Hello World! to the console, and then quickly shut down. To pause the program we can ask the program to request that the user hit a key.
...staticvoidMain(string[]args){Console.WriteLine("What is your name? ");stringname=Console.ReadLine();Console.WriteLine("Hello {0}!",name);Console.ReadKey();// wait for user input}
Note: Double forward slash (//) is one method for creating comments in the code. Any text after the // is ignored by the compiler until the next line. Comments are used for many reasons. Here we explain the purpose of code that may be unclear just by reading the code.
Congratulations! We've created a fully functional C# program using the console. Check outWhere To Go Next to find the next lesson.
Using Visual Studio 2003, we will create the famous "Hello World!" example in CSharp (Also referred to as C#).
First Create a new project.
Do this by following these steps:
1) Start Visual Studio
2) Select the Menu item: "File"
3) Select the Sub-Menu item: "New"
4) Select the Sub-Menu item: "Project"
5) Select Project Types: "Visual C# Projects", Selecting Templates: "Windows Application"
6) For Name: type "HelloWorld", then select the 'OK' button.
Congratulations you have created a new project!
Now, let's create our "Hello World!" example.
1) In the Solution Explorer on the right, you will notice your "HelloWorld" project has several files. Using your mouse, double-click the Form1.cs file. You will see that Form1 now displays in your viewer in [Design] Mode.
2) Now Select this [Design] mode Form1 with your mouse. Right-click and select "Properties".
3) You will see a list of what are called Events. These events are messages your windows form can respond to. Select the "Load" event and double-click this.
4) This will take you to [Code] Mode. You will see the following code displayed after double clicking the "Load" event:
privatevoidForm1_Load(objectsender,System.EventArgse){}
5) in between the two curly braces (ie: "{ }") type the following:
MessageBox.Show(this,"Hello World!");
6) Here's what you should see:
privatevoidForm1_Load(objectsender,System.EventArgse){MessageBox.Show(this,"Hello World!");}
7) Run the program to see "Hello World!" by Pressing and holding down the two keys: CTRL F5
8) This will start your program and you will see a message box display "Hello World!"
Congratulations you have made your first C# program!--Darshan Arney 21:58, 17 October 2006 (UTC)
| Topics inC# | ||
| Beginners | Intermediate | Advanced |
|---|---|---|
| Part of theSchool of Computer Science | ||