C Sharp/First Program

From Wikiversity
Jump to navigation Jump to search

Hello World[edit | edit source]

Using Visual Studio 2008, we will create the famous "Hello World!" example in a C# console application.

  • Create a new project (File->New->Project)
  • Select Visual C# in Project types
  • Select Console Application from the Visual Studio installed templates
  • Click OK

Now we're ready to write some code. The project should have a file called Program.cs. Open that file to edit the code that will execute.

Program.cs[edit | edit source]

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Note that you don't need the string[] args part of your loop. In fact, you can omit it like so:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
        }
    }
}

Note: In the end of your C# program, for VSCode or Visual Studio, you need to add Console.ReadKey(); or else the program will close after executing the last line of code in the static void Main() or static void Main(string[] args) loop.

Remarks[edit | edit source]

using System;

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).

namespace ConsoleApplication1

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.

static void Main(string[] args)
static void Main()

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.

Console Output[edit | edit source]

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.

...
static void Main(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.

...
static void Main(string[] args)
{
    Console.WriteLine("What is your name? ");
    string name = 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 out Where To Go Next to find the next lesson.

Hello World with Windows Forms[edit | edit source]

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:

  private void Form1_Load(object sender, System.EventArgs e)
  {
 
  }

5) in between the two curly braces (ie: "{ }") type the following:

   MessageBox.Show(this, "Hello World!");

6) Here's what you should see:

   private void Form1_Load(object sender, System.EventArgs e)
   {
       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)

Where To Go Next[edit | edit source]

Topics in C#
Beginners Intermediate Advanced
Part of the School of Computer Science