Using Notepad++ to write C# code

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.

Features

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

C# Language

Auto Completion

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.

Notepad++ auto completion

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.

C# Auto Complete XML

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

Compilation

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.

Hello World Completed Code

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:

csc HelloWorld.cs

Visual Studio Developer Command Prompt CSC Command

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

Visual Studio Developer Command Prompt CSC Command With Outputs

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.

Hello World

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.

Advertisements

About Ken Cenerelli
I am a Programmer Writer, .NET Developer, Microsoft MVP - Visual Studio and Development Technologies, Public Speaker, Blogger, and Microsoft Azure Nerd. I blog regularly at kencenerelli.wordpress.com and can be found on Twitter via @KenCenerelli.

6 Responses to Using Notepad++ to write C# code

  1. blupilot says:

    Thanks, this helped out a lot!

  2. susu says:

    Thanks that was really useful! I also used this website to be able to execute CSC.exe from any command prompt: http://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/

    The two paths I had to add were:
    C:\Windows\Microsoft.NET\Framework\v4.0.30319
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\SDK\v3.5\Bin

  3. Thanx for the tutorial, very useful article. Didn’t know about c# compiller before))

  4. Pingback: 2015 year in review | Ken Cenerelli

  5. Pingback: 2016 Year in Review | Ken Cenerelli

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: