Hello C# 002

I'm literally writing this for my brother, someday he will read it.

Short format

Due to the lack of proper time to sit and write a longer text, I'm switching into a short format.
In the short format the REPL is going to be used instead of proper projects.

In the beginning

We are going to begin a journey in the ladders of abstraction with the array of integers.

At the hardware level, memory is represented usually as sequences of bits.
While programming in assembly languages arrays are basically all there is.
Higher level languages introduces newer ways to reason about data in memory but we are going to cover those latter.


To create an array of integers in C# we use the following snippet:

> var memory = new int[128]; // 128 is the array size.

With the array created we can simulate a program that sums 2 and 3 like as follows:

> memory[0] = 2
> memory[1] = 3
> memory[2] = memory[0] + memory[1]

The square brackets syntax allows us to reference to an element of the array.
Because C# is a 0-based index language, the first element of the array is at index 0, the second at index 1 and so on.
Some languages provides 1-based indexing, but this is not very common.

Because the index is also a number, we can use values from the array to index things:

> memory[memory[0]] = memory[1]

And that is all for today!

[Try by yourself] Create an  array of size 10 and try to set the value at index 10 to 42. Does it work? Experiment negative values for the index.

[Try by yourself] Relative indexes are very common. Experiment the following snippet and reflect about it:

 > memory[0] = 10
> memory[memory[0]] = 1
> memory[memory[0]+1] = 2
> memory[memory[0]-1] = 10

Up Next


Go home.

☆Powered by Azure Blob