1. C# Compiler
Computers don't understand human language. Heck, they don't even understand programming languages. They only get programs written in machine code — just a bunch of zeros and ones.
Dogs have commands like "Heel", "Paw", and so on, and when they hear them, they do something important. For computers, numbers play that role: every command is encoded as a number (also called machine code).
Writing a program as a bunch of numbers is super hard, so people came up with programming languages and compilers. This kind of language is understandable for humans on one side, and for the compiler on the other. A compiler is a special program that translates the text of your program, written in a programming language, into a set of machine codes.
Usually, a programmer writes a program in a programming language, then runs the compiler, which takes the files with the code and makes a single file with machine code — the final (compiled) program.
- Program in C++
-
Compiler
- Program made of machine codes
The resulting program can run on the computer right away. The downside is that the code of the program depends a lot on the processor and operating system. A program compiled for Windows won't work on an Android phone.
If you wrote a program for Android, it won't run on Windows!
But C# has a way more innovative approach.
- Program in C#
-
C# Compiler
- Program made ofspecial IL-codes (byte-code)
-
CLR
- Program made of machine codes
The C# compiler doesn't compile all classes into one machine code program. Instead, it compiles your code not into machine code, but into a special intermediate IL-code (byte-code). The compilation to machine code happens when you run the program.
So who compiles the program into machine code when you run it?
There's a special program for that called CLR (Common Language Runtime) — the C# virtual machine. First, you launch it, and then your program made of byte-code. And then CLR, before running your program, compiles it into machine code.
If you have a fancy processor that supports more machine commands, then during this "second compilation" the machine code will be generated specifically for your processor and OS. That's why C# can sometimes be faster than C++, which is compiled straight to machine code and can only use the most common processor commands.
2. Typical Compilation Errors
Speed and code optimization aren't super important for us right now, but what is important is that the compiler checks your program for errors. It checks your code for correctness and won't let it through if it finds even a tiny mistake.
Error Example:
You're trying to assign a number to a variable that can only store text.
string userName = 42; // Oops! You can't do that.
The compiler will immediately throw an error that the types don't match: "Cannot implicitly convert type 'int' to 'string'".
Another example — a typo in the WriteLine() command:
Console.WriteLin("Hello!"); // Method name typo
The compiler will say: "No such method — check your syntax!"
How to read errors?
Most compilers tell you where the error is in your code, and sometimes even suggest how to fix it. Don't be afraid of errors: each one is a step toward enlightenment and beating your inner "boss".
3. Comments
So, imagine you're writing a complicated program. Everything works, you're a genius! But a month later you open that code and... you don't get a thing. A couple lines here, a weird variable there. All you can think is: "Clearly, anyone but me wrote this!" That's where comments save the day.
Comments are like notes in the margins of a textbook or sticky notes on the fridge ("Beer. Don't drink. For tomorrow!"). The compiler ignores them, but people don't. Comments let you explain what your code does, mark important details, and even "turn off" parts of your program without deleting them.
Important: No compiler reads your comments, they're totally ignored!
Single-line comments
In C#, there are two types of comments — single-line and multi-line. Single-line comments start with double slashes // and go to the end of the line. Everything after // is considered a comment and is totally ignored by the compiler.
Example:
Console.WriteLine("Hello, world!"); // This prints a greeting to the screen
Here // This prints a greeting to the screen is a comment. If a line of code seems unclear, just write a comment like this!
Another example:
int x = 42; // Programmer's age value
You can also write single-line comments on their own lines:
// Initializing the user name variable
string userName = "Vasya";
It's almost like lines in a script: the comment explains what and why.
Multi-line comments
What if you have a lot to say? Like a real story? That's what multi-line comments are for, which start with /* and end with */. Everything between these symbols is ignored by the compiler, even if it's several lines.
Example:
/*
Here we print the user's name to the screen.
If the name is missing, we'll write "Guest".
*/
Console.WriteLine("Enter your name:");
Or like this:
int age = 18; /* Initial age value
for a new user */
Heads up: you can't nest multi-line comments inside each other. Don't try to outsmart the compiler — it won't work.
Illustration:
| Type | Syntax | Example |
|---|---|---|
| Single-line | // comment |
int x = 1; // explanation |
| Multi-line | /* stuff here */ |
/* you can explain whatever you want here */ |
4. Real-life Examples
"Turning off" a line of code:
Sometimes you want to temporarily not run a piece of code. Comments are perfect for that:
// Console.WriteLine("Text that's not needed right now");
The line is still there, but the compiler doesn't "see" it.
TODO and FIXME notes
Programmers are people too, and sometimes you need to remind yourself that some code isn't finished yet, or that there's a bug here. For that, people often write:
// TODO: add a check for empty input
// FIXME: this function calculates the sum wrong
Lots of editors and IDEs even highlight these words!
Programmer jokes
And of course, sometimes you find really funny comments in code:
// I'm not responsible for this code. I was forced to write it against my will.
// Dear, I_am_from_the_future! Please forgive me for this code.
// If I ever see this again, I'll have to start bringing a weapon to work.
// If this condition ever happens, please call me at xxx-xxx-xxx for a reward.
// Dear programmer:
//
// When you finish "optimizing" this subroutine
// and realize what a huge mistake that was,
// please increment the counter below as a warning
// for the next guy:
//
// hours_spent_here = 42
// When I started writing this, only God and I knew what I was doing.
// Now only God knows
// Sometimes I feel like the compiler ignores all my comments
// I dedicate all my code, all my work to my wife Darlin, who
// will have to support me, our three kids, and the dog when
// this goes to production.
Yeah, comments can be really funny. After all, they're written by real people.
GO TO FULL VERSION