Writing and compiling code using the Developer Command Prompt
When you install Visual Studio, other tools are installed too. One of those tools is the Developer Command Prompt for VS2015 that has its path set to find developer tools such as the C# compiler.
Writing code using Notepad
Start Notepad (or your favorite text editor) and enter the following code:
class Program { static void Main() { System.Console. WriteLine("Hello C#!"); } }
You can type the code all on one line or spread it out over multiple lines and indent your lines to make it easier to read.
Note
C# is case sensitive, meaning that you must type uppercase and lowercase characters exactly as shown in the preceding code. C# is not whitespace sensitive, meaning that it does not care if you use tabs and spaces and carriage-returns to lay out your code however you like.
From the File menu, choose Save As.
In the dialog box, change to drive C:
(or any drive that you want to use to save your projects), click on the New Folder button, and name the folder Code
.
In the Save as type field, select All Files from the drop-down list to avoid appending the .txt file extension, and enter the file name as myfirstapp.cs
, as shown in the following screenshot:
Your code in Notepad should look something like the following screenshot:
Compiling code using the Developer Command Prompt
Start the Developer Command Prompt for VS2015 by typing the letters deve in the Windows 10 Search box, as you can see in the following screenshot. You will also find it on the Start Menu or Start Screen listed in All apps in the Visual Studio 2015 folder:
At the Command Prompt, enter the commands to do the following:
- Change to the
C:\Code
folder - Compile the source code using the C# compiler
- Request a directory listing
- Run the application by entering the name of the EXE
Here are the commands:
cd C:\Code csc myfirstapp.cs dir myfirstapp
The output in the Command Prompt window should look like this:
Note that your source code, the file named myfirstapp.cs
, has been compiled into an assembly named myfirstapp.exe
. When you enter the name of the assembly, it is loaded and executed by .NET Framework 4.6 and its CLR.
You can copy the myfirstapp.exe
file onto any computer with Windows 10 and it will run correctly because all copies of Windows 10 have .NET Framework 4.6 installed. Actually, you can copy the file onto a computer with an older version of Windows as long as it has .NET Framework 4.0 or later installed.
Fixing compiler errors
If the compiler displays errors, read them carefully, and fix them in Notepad. Save your changes and recompile.
Tip
At the Command Prompt, you can press the up and down arrows on your keyboard to cycle through previous commands you have entered.
A typical error might be using the wrong case, a missing semi-colon at the end of a line, or a mismatched pair of curly braces. For example, if you had mistyped a lowercase m
for the Main
method you would see this error message:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Decompiling code using ILDASM
The C# compiler converts your source code into Intermediate Language (IL) code and stores the IL in an assembly (a DLL or EXE file).
IL code statements are like assembly language instructions, but they are executed by the .NET virtual machine known as the Common Language Runtime (CLR).
At runtime, the CLR loads the IL code from the assembly, JIT compiles it into native CPU instructions, and then it is executed by the CPU on your machine.
The benefit of this two-step compilation process is that Microsoft can create CLRs for Linux and Mac OS X as well as for Windows. The same IL code runs everywhere because of the second compilation process that generates code for the native operating system and CPU instruction set.
Regardless of which language the source is written in, all .NET applications use IL code for their instructions, stored in an assembly. Microsoft provides a tool that can open an assembly and reveal this IL code.
Tip
Actually, not all .NET applications use IL code! Some use the new .NET Native compiler to generate native code instead of IL code, improving performance and reducing memory footprint, but at the cost of portability.
Disassembling compiled assemblies
Disassembling a compiled assembly is an advanced technique. I will walk you through the process, but do not worry about fully understanding what you are seeing yet!
Start the IL Disassembler by entering the following at the Developer Command Prompt:
ildasm myfirstapp.exe
You will see the IL DASM tool with the compiled EXE assembly loaded:
Double-click on
MANIFEST in the tree view to show the metadata version (4.0.30319) of .NET and the Microsoft Core Library (mscorlib
) assembly version (4.0.0.0) that this assembly needs to run. The following screenshot tells us that to run this assembly, we would need .NET Framework 4.0 or later installed:
Close the MANIFEST window, expand Program, and then double-click on the Main method. Note the IL instructions: ldstr
(load string), nop
(no operation), and ret
(return). Remember that IL is an assembly language that is executed by the .NET CLR:
Anyone can use this tool to see any .NET assembly's IL code. Don't panic! This is not a reason to avoid C#.
All applications are lists of instructions that must be visible to the machine that runs it. If the machine can read these instructions, so can anything else. Therefore, all software can be reverse-engineered. It's just a matter of the effort required. .NET just happens to make it very easy!
Tip
ILSpy is a popular open source tool that does everything IL DASM does and can also reverse-engineer the IL code into C# or Visual Basic .NET source code. I used this tool for a client who had lost the source code for an assembly and needed to recover it!