Uncategorized Home @ it-notebook.org

Your first application in C#

(Kristofer Gafvert, April 3, 2004; Updated: January 15, 2005)

Introduction

This article will help you to write your very first application in C# (pronounced "see-sharp"). To follow this article you will need a text editor. Good old notepad will work, but I recommend a text editor with at least syntax highlighting and auto indent. These two features will make coding much more fun. For small application such as those I will present in this article, I use NotePad++. It is a great, small text editor.

No, you do not need Visual Studio .NET to write .NET Applications. Many people think so, but this is not true. An IDE (Integrated Development Environment) will help you to write code in the future, but when learning how to program, I usually recommend a simple text editor.

Good luck, and now, let's get started!

Your first application

In this chapter you will write your first application. I will also give you some basic troubleshooting guidelines, if your application do not compile correctly.

Hello World

It would probably be a good idea to start with a simple "Hello World"-application (couldn't resist to start with the classic one).


using System;

namespace HelloWorldApplication
{
   /* The Hello World class */
   class HelloWorld
   {
      static void Main(string[] args)
      {
         //Write "Hello World" to the screen
         Console.WriteLine("Hello World!");
      }
   }
}

Save this code as HelloWorld.cs (if you are using NotePad, make sure that you do not get the extension cs.txt). After you have saved the file, open a command prompt (Start->Run, type ?cmd?). Go to the folder that you saved the file to, and write:

csc HelloWorld.cs

If you are lucky, you will get this screen:

This one tells you that your code compiled, and that you are ready to run the application. To run the application, type:

HelloWorld.exe

If everything went as expected, it should now write "Hello World!" on the screen.

It is not working!

What if it did not work as I said? Don't worry, I will tell you about some common errors.

'csc' is not recognized as an internal or external command, operable program or batch file.

If you get the above message, there are two things that could be wrong. The first one is that you have not installed the .NET Framework SDK. In order to run and compile the applications we write in this tutorial, you need the .NET Framework SDK. You can download it from http://go.microsoft.com/fwlink/?LinkId=8684 . Make sure that you download the latest edition of .NET Framework SDK.

If you have Visual Studio .NET installed, you already have what you need installed, but you will still get the same error message. Start the Visual Studio Command Prompt in the Visual Studio .NET Program Group, and it will work.

If you still have the above problem after installing the .NET Framework SDK, the folder where the csc executable is located is probably not included in the PATH environment variable. To fix this, follow these steps (for Windows XP, if you are using another edition of Windows, and the steps are not the same, please use the Windows documentation to find out how to do this):

  • Right click "My Computer" and click Properties
  • Click on the "Advanced" tab
  • Click the button "Environment Variables"
  • In the "System Variables" section, find PATH. Click on it, and click on the "Edit" button
  • Write: ;%systemroot%\Microsoft.NET\Framework\v1.1.4322 at the end of the list. The string starts with a semicolon (that is used to separate the strings in the PATH variable), and make sure that you type the correct version (when you read this, the version I wrote might not be the latest one).
  • Click "Ok" three times, and now start a new command prompt.
  • Compile again, and this time it should work

It is also possible that you get a compile error. Say for example that you forgot to end the line that writes to the console, with a semicolon. Then you would get an error similar to this one:

HelloWorld.cs(11,43): error CS1002: ; expected

This tells you that you have an error on line 11, column 43, and that the compiler thinks that you forgot a semicolon. Note however that you cannot always trust what is written here, the compiler could be wrong about what in your code is wrong. As a general rule, start where the compiler says that you have an error, and then walk upwards, until you find an error. If the compiler says that you have several errors, start with the first one, all other errors might be fixed automatically by this :-)

The code

So, what did we really do? Let's walk through the code, so that you get a brief introduction to C# coding.

If you have previously used C++, you would probably notice that this simple C# code already involves classes, whereas in a C++ programming book, you would not see classes until chapter 5 (or something like that). C# is an object oriented language, and everything must be written in classes. It is not possible, like in C++, to have a Main method without a class. If you try to remove the class, and compile the Main method only, you will get an error similar to:

HelloWorld.cs(8,33): error CS1518: Expected class, delegate, enum, interface, or struct

The namespace HelloWorldApplication contains our class HelloWorld. A namespace can be compared to a package in Java. A namespace is simply a way to group elements (classes, other namespaces) that belongs together. It is also a way to distinguish two classes with the same name. For example, company A writes their own HelloWorld class, whereas company B writes another HelloWorld class. It would not be possible to use these both classes if they did not exists in different namespaces; because the compiler would not know which class we wanted to use.

As you can understand, putting everything in different namespaces can result in quite lengthy names. And more words, means more spelling errors. So of course, there is a way to write less, we can use the "using" directive, as we do on the first line.

"Console" is a class in the "System" namespace, and instead of "using System", we could have written the fully qualified name:

System.Console.WriteLine("Hello World!");

This would give us the very same result, but why write "System" all the time when we do not have to?

You have probably also noticed Main with a capitalized M. This is not a typo! A design guideline (for C#) is to begin all method names with a capitalized letter, and this applies to the Main method as well. So if you are a C++ developer, do not forget that this differs! And as you have probably already guessed, C# is cAsE sEnSiTivE.

You can also see two different kinds of comments in the example code. Exactly as for C++, you have the single line comment that starts with //, and the multi line comment beginning with /* and ends with */. C# also has one other set of comments, the single line comment /// and the multi line comment that starts with /** and ends with */, that can generate HTML documentation when you run csc with the doc switch.

So, that is it! You have written your first application using C#, and you hopefully understand most of the code.

Applies to [?]

C# 1.1
C# 2.0

See also

Download code, CSharpFirstApplication-Code.zip