Notepad++ is a free source code editor that runs on the Windows operating system. It has become the standard text editor on most developer’s machines and it has replaced the lowly Notepad tool that comes with any Windows OS.
Notepad++ is not only my favourite text editor but it is my favourite tool for development after the Visual Studio IDE.
It is very versatile in looking at all types of files and it has many programming language syntax highlighters built in. It also supports many plugins that can make tedious tasks very simple. Some of my favourites are Compare (for file comparisons), NppFTP (for moving and editing files on different systems) and JSON Viewer (for formatting JSON).
Although I have explored many files with the program I have never tried to write a complete Hello World in C# with it before. This article will explore some of the neat things that you can do with Notepad++ and how we can leverage the tool to write a simple program. We will then compile it using the C# compiler that is available on the Visual Studio Developer Command Prompt.
With Notepad++ you get some of the same features you would with the Visual Studio IDE including:
- Support for C# keywords including color coding
- Collapsing of methods and regions to make examining code much easier
- Auto completion (similar to IntelliSense) for C# keywords and .NET namespaces
To enable syntax highlighting for your Hello World project make sure you set the language to C#.
To enable auto complete while coding in any language within Notepad++ press the CTRL + Space bar keys to see the list. You can then arrow up and down within the list to see your options. From there you can hit Enter to insert your selection into the document.
Developers can also modify and extend the auto completion options available to them by editing the XML file associated with the language they are working in. You can find this file in the Notepad++ install directory on your computer. Normally this would be the default location of C:\Program Files (x86)\Notepad++\plugins\APIs\
Below you can see what the C# file (cs.xml) looks like when it is opened.
As you can see from the list both of the new .NET 4.5 keywords for Async and Await are not available to the user. You could simply add them here to start working with the newest functionality of .NET within Notepad++.
Let’s now return to our Hello World program that we are writing. Notice I have added a second Using statement to the class file below to allow me to raise a message box as well as print the results out to the console. To compile this file you must save it as a *.cs file. You will see I named mine HelloWorld.cs.
Now open up the Visual Studio Developer Command Prompt and cd into the directory where your saved file is located.
For this next part we will use the command line C# Compiler to make our code executable. The C# Compiler allows us to understand how C# compiles our code and it gives us a deeper understanding of the .NET Framework. While I would not use it for huge projects it is nice to know I can create a C# project without the Visual Studio IDE.
From the command prompt you can run the csc –help command to see the full list of options available to the developer. For general purposes though you will only need a few items:
- /out allows you to set the file name of the assembly to be created. If not specified then the output is the same as the initial *.cs file name
- /target:exe allows you to set the default assembly output to an executable. This is the default and it can be removed for this type of application
- /target:library allows you to set the assembly output to a *.dll
- /target:winexe prevents the console application from appearing in the background
We can now compile our HelloWorld.cs file into an executable. To do this we can simply enter the following into the command prompt:
We will produce a file in the same locale named HelloWorld.exe.
If we want to be more verbose or if we want to change output names then we can do the following:
csc /target:exe /out:HelloWorld_New.exe HelloWorld.cs
This will produce a file in the same locale named HelloWorld_New.exe.
If we run either of these Hello World executable files we will get “Hello” written to the console and “World” written to a message box.
In this article I have showed you how to create a C# class file and how to compile it into a Hello World application. Hopefully you have found this blog post useful. If you have other Notepad++ tips please leave a comment below as I would love to read them.