Entry Level5 Find the basementC# compilation process

Basement for a programmar

Where Is the Basement?

Now that I had the code for the basement, I just needed to find the basement and figure out how to use the code. In all my time at the base I had never heard anything about a secret storage area, so I knew I had some sleuthing to do. I figured I'd start by taking the elevator to the lowest floor it accessed and then searching for some sort of inconspicuous back stairwell that would lead me even deeper.

I walked calmly to the elevator, pressed the down button, and was relieved to see that I was the only one in the hallway. When the doors opened, my luck continued and I got on for a solo ride down. I looked over to the button panel and, to my surprise, there was an actual Basement button. Maybe I wouldn't have to sneak around to find the entrance after all!

After about 30 seconds, the elevator stopped one floor above the basement. I should have known that it couldn't be as easy as pressing a single button. The number on the screen above the door disappeared and a message appeared: "Only base leaders can access the basement. You will be given a quiz about .NET Framework, C#, and JIT compilers. Fail the quiz, and lasers will split you into atoms. Have a nice day!" The message may as well have been written in a different language. Except the lasers part - I understood that loud and clear. Having second thoughts, I frantically punched the numbers on the button panel, willing the elevator to realize that I would prefer to be anywhere but there at the moment. Thankfully, it registered before the quiz could start, and the elevator just happened to deliver me to the floor that I lived on. I had been rushing headlong into this secret quest, but the threat of death by lasers can change even the most stubborn of wills.

Never divide by zero

Back at my room, I entered a local wiki. It had a page entitled "Advanced C# info," there was a part of the page, available to read, and another one, that opens if you solve the task. The first part stated: "In c# you can NOT divide by zero. If your program divides by zero, it terminates with a runtime error. Here are some examples:"

Console.WriteLine(12 / 0); // ERROR! Can't divide by zero!

int a = 10 / 0; // ERROR! Can't divide by zero!

int b = 10;
int c = 0;
int d = b / c; // ERROR! Can't divide by zero!

You can avoid dividing by zero, using if :


    int a = int.Parse(Console.ReadLine());
    int b = int.Parse(Console.ReadLine()); // b can be 0, we don't know
    int quotient = 0;

    if (b != 0)
    {
      quotient = a / b;
    }
    else
    {
      Console.WriteLine("Can't divide by zero!");
    }

To continue to the next part, I solved the task:

After solving the task, the wiki delivered me to a page filled with definitions and explanations of various terms and acronyms, including, to my relief, .NET and JIT . I got myself a tall cup of (still milkless) coffee and settled down to pore over the material.

Compilers, Runtime, and the .NET Framework

Have you ever wondered what happens to your C# code when it runs on your computer? Probably not, but it's worth a look.

A long time ago, when computer programming was in its infancy, programmers wrote programs in machine codes (language that the processor could understand). It looked like this:
...
b8 21 0a 00 00
a3 0c 10 00 06
b8 6f 72 6c 64
...
Not exactly the easiest read, right? When writing machine codes, you tell the processor the exact address of the memory location at which to read and write data. Every command is represented by a number, and because that isn't complicated enough, commands differ among different processors.

To avoid this complexity, clever people started to invent abstractions and write programs on a higher level than the raw machine code. They designed programming languages that allow you to write code without worrying about the under-the-hood stuff like memory management, how to handle different hardware, and many other things that we as high-level programmers may not even know about.

Ooperating_systems

Modern software developers usually work on a high level of abstraction. In fact, a colossal percentage of programmers don't know the full extent of how their code runs all the way down to the processor level; they know just enough to operate on their own level. Thanks to high-level programming languages, programmers can code effectively without needing to understand every little detail about the computer. It's up to the programmer (in effect, YOU!) to figure out how far down they want to dive in learning how computers translate their code into functioning programs. As your learning here continues, we'll provide you with the basics that will help get you coding and running your programs.

The first thing you need to know about is a compiler. A compiler is a program that transforms source code written in one programming language into another programming language. "Why would I need this?" you ask, "I thought we were writing in C#?" The answer goes back to our earlier discussion about machine codes - the C# code that you write must be converted into something that your computer's processor can understand. The processor is, of course, the brain of the computer - the part that performs all its calculations. The processor doesn't know C#; it only understands machine codes.

Okay, let's zoom back out to the high level, where you just finished writing your C# code. When you run the C# compiler, it takes your code as an input, does some processing, and then outputs your program in intermediate language (IL) code which is saved in *.exe or *.dll files.

Learn c# .net structure

This is not the end of the code permutations that take place under the hood. After the IL code is generated, we still don't have anything for our processor to read (remember, the processor can only understand machine codes). Someone or something, therefore, needs to convert the IL code to machine codes. The name for that something is the Common Language Runtime or CLR.

CLR is a program running on your computer that manages the execution of IL code. In simple terms, it knows how to deal with IL and how to execute programs that are written in IL code. It uses a just-in-time, or JIT, compiler to translate the IL code into machine code (sometimes called "native" code). The JIT compiler is named as such because it compiles IL code at the moment that the user tries to use it; for example, when you double-click the .exe file.

Learn c# .net structure expanded

Fortunately for us, both the CLR and JIT compiler know what hardware the computer consists of and compiles IL into a native code that the computer is comfortable with. This also provides some amount of future-proofing: if you write a program and compile it with the C# compiler today, and then forget about it for... say... 10 years, that same .exe file will most likely still run (and probably even faster than it would have today)! The JIT compiler would still look at the hardware of the (future) computer, determine what that computer could understand, and translate the IL code (in your 10-year-old .exe file) into that language and run the program.

To boil it down, let's take a look at the path your code takes from being written to being run:

c# program execution

Of course, your code may not always run flawlessly. There are two types of errors that can occur in your program: compiler errors and runtime errors.

Compiler errors are found by the C# compiler and prevent your code from being compiled into a .exe. Some common examples of compiler errors are missing semicolons or misspelled commands. The C# compiler reports these errors and stops processing the output because doesn't know what to do with the error. The compiler will write these errors to the output window of your IDE, and the output is your key to understanding what went wrong and needs to be fixed. You should always READ COMPILER ERRORS!

Runtime errors occur if an issue is encountered while the program is running. These are usually serious errors that the runtime halts at because continuing could impact other files or the operating system. Possible runtime errors include a program trying to divide by zero or writing to a read-only file. Runtime errors are much more dangerous than compiler errors, which is why developers try to make sure the compiler catches as many errors as possible at the compile stage before your code becomes an executable program.

In order to run C# programs, you need a basic class library, a CLR, and any additional libraries your code may use. Don't worry about finding them all separately - these are all part of a package called .NET Framework. The first beta version, designed specifically for Windows, was released in 2000. If you want to write C# code on Linux or Mac OS, you can use other frameworks such as Mono or .NET Core.

After you have .NET Framework installed, you can run programs that were written using a wide variety of programming languages:

Quiz

I read and re-read the terminology until I felt I could recite it by heart. After another shot of weak coffee to boost my courage, I headed back to the elevator and hit that Basement button again. Yep, still a terrifying threat of death by laser. I was fairly confident this time around though, and I managed to stand still as a big laser cannon appeared from the ceiling and angled itself toward me. A small touch screen popped out of the panel opposite the buttons and gave me the following tasks:

a staircase that is spinning rapidly

The laser cannon retracted back into the ceiling panel. Wow! It wasn't so hard! The doors slid open and I walked down a short corridor and began descending a flight of stairs. This must lead me to where I can input the secret code!
Suddenly, the stairs began spinning. Faster and faster.

I was trying to figure out if it was real or not when an electronic voice broke my concentration.
"To stop this, prove that you have the secret knowledge."

I did it, I solved all the tasks! The world finally stopped spinning and whatever had just happened deposited me at the bottom of the stairs, facing a wall. An old 10-digit keypad was mounted there, and I entered the secret code, 145. There was a grinding noise, and the whole wall rotated revealing what was hiding in the basement all this time.

Unlock