My first C # program: the best tips
Related Videos: Writing your First C Program and Running it - C Programming Tutorial for Beginners (May 2024).
This practical tip is suitable for beginners and shows you how to create your first program with C #.
Preparation for the first C # program
In order to be able to write your first C # program, you need the ".NET framework" on your PC. If you use Windows and regularly install all updates, the latest ".NET framework" should already be on your computer. Otherwise, you can check it using the following steps:
- Open the control panel.
- Click on "Programs and Features".
- In the overview you should now find ".NET Framework 4.x". If this is not the case, you can download it here.
Write the first C # program
There are several options for your very first C # program:
- You can write the program with a normal text editor and then run it with the compiler included in the ".NET Framework".
- Alternatively, use the "Visual Studio Express" development environment provided free of charge by Microsoft. We will present both variants in this practical tip.
C # programs with text editor
A simple text editor such as "Notepad", which is included as standard in Windows, is sufficient to create a C # program. You get a little more convenience with more extensive text editors such as Notepad ++. The various commands are highlighted in color and the source text is automatically formatted.
- Open the editor and copy the source text from our picture gallery.
- Save the file on your PC, for example with the name "ErstesCSharpProgramm.cs". Make sure that the file extension is ".cs" so that the compiler notices that it is a C # program.
- Now you have to call the compiler. You can usually find this in the directory "C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319". In this directory there is the file "csc.exe", which converts your C # code into an executable file.
- Now call the "Run" menu via "Start"> "Run" or the shortcut [Windows key] + [R] and enter "cmd". In the command prompt you must now change to the compiler folder with the command "cd" C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 "". Pay particular attention to the quotes around the file path.
- Now enter the command "csc" together with the path to your C # file, for example: "csc" C: \ Users \\ Desktop \ ErstesCSharpProgramm.cs "". You should return to the prompt after a short time without an error message.
- In your target directory you will find the file "ErstesCSharpProgramm.cs" as well as a file "ErstesCSharpProgramm.exe". You can now run this and see the result of your program.
Write C # programs with Visual Studio Express
Microsoft's free development environment makes it easier to write your first C # programs. Best of all, you don't need any other programs after installing to write and execute your code.
- First, start Visual Studio Express.
- Click "File - New - Project" and select "Console Application" in the next window. Also enter a name for your project. Then click on "OK".
- As you can see, Visual Studio is already preparing the basic structure for your program code, which you still had to write yourself in the text editor. To complete our program, you only need the line of code "System.Console.WriteLine (" Hallo Welt ");".
- Now run your program using the "Start" button or the key combination [CTRL] + [F5].
- Visual Studio starts the compiler in the background, compiles the program and outputs it in the command prompt.
Conclusion - my first C # program
Whether with a text editor or Microsoft development environment - you can quickly create the first C # program in both ways. For beginners, it is recommended to first use the editor instead of Visual Studio so that you can become more familiar with the programming language. There is also free reading for anyone interested who would like to continue working on C #.
If the first C # program was too complex for you, you can try something simpler. In the next practical tip, read how to create your own joke program.