Hello C# 001

Hey brother, practice and consistency may not beat talent, but surely it gives a good punch.

Getting started

  1. Learn how to use a terminal/shell.
  2. Get .Net (dotnet) SDK.
  3. Get a text editor, here I'll be using micro.
  4. Create a folder to keep all the code we are going to use.

Backup PDF with the documentation of the language here (source: Microsoft).

Hello World

PROJECT: 001-HELLO (final)

As our first bit of code, create the folder 001-HELLO and execute the command % dotnet new console inside it.

This is going to create some files and folders in the folder. Of those, the interesting bits are named 001-HELLO.csproj and Program.cs.
Take a look using your text editor, the .csproj holds information about how to transform the other file into a program and the .cs file contains the hello world itself.

You can use the command % dotnet run to build and run you project. You're going to see the Hello, World! message in your terminal.

Code and program

Things is the computer are a very long series of 1's and 0's. Programmers write code that describes programs. This code might be directly executed in some cases and/or might be transformed into another format which the system is capable of processing. A third option is to have an intermediate format, which is not directly execute nor is usable for direct execution.

As this bit can be expanded indefinitely, let's cut dry. In this series we are going to code in C#, C# programs are not directly execute in general, not it's transformed into machine code. Instead it's generated an intermediate format designed to be execute by a imaginary machine. And another program does the job to execute this intermediate format, either by directly processing bits of it or by generating native machine code that is executed on the running system.

So code always describes a program, but it by itself might not be a proper runnable program to the system. A collection of 1's and 0's is called data which is the plural of datum.

Checking

This might be different on your machine, but running a listing program on the project 001-HELLO folder after running it I have the following structure:

001-HELLO
|--001-HELLO.csproj
|--Program.cs
|-/bin
|  |-/Debug
|     |-/net6.0
|        |--001-HELLO
|        |--001-HELLO.dll
|        |--...
|-/obj
   |-...

The ./bin/Debug/net6.0/001-HELLO.dll is the intermediate format that can be execute by invoking % dotnet ./bin/Debug/net6.0/001-HELLO.dll and the ./bin/Debug/net6.0/001-HELLO is a basic native program that magically runs the .dll file.

REPL

We haven't touched code yet and we are going to evade it until the end of this section. REPL is a kind of program that interprets code as the user types it instead of processing a complete program. It's very useful while learning and testing things which is our case. I recommend this C# REPL, but you may use others.

Hello again

By reading the Program.cs file you may find something along those lines:

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

The line with // is just a comment while the second one is a function call that makes the "Hello, World!" appear on the screen.

If we write this line to our C# REPL we are going to get the same result:

% csharprepl
Welcome to the C# REPL (Read Eval Print Loop)!
Type C# expressions and statements at the prompt and press Enter to evaluate them.
Type help to learn more, and type exit to quit.

> Console.WriteLine("Hello Again");
Hello Again

> exit
% ...

[Try by yourself] You might be pleased to find that if you omit the ; at the of the line nothing will change. This is because of the fortunate fact that the REPL interprets expressions and statements, but removing it from the Program.cs file will lead to an error while trying to build the program.

Variables

C# is a big language, not as ginormous as C++ but big enough that we can't cover everything in one go if you're a beginner. For this reason we are going to exploit the fact that REPL accepts expressions.

The first thing to know about is that everything after a // and before the end of the line or between /* and */ is in fact ignored by the C# compilers and we call those bits comments.

C# allows us express number values by simpling writing it. The same is valid for strings of characters which are enclosed by double quotes.

% csharprepl
Welcome to the C# REPL (Read Eval Print Loop)!
Type C# expressions and statements at the prompt and press Enter to evaluate them.
Type help to learn more, and type exit to quit.

> 42 // this is a number
42
> "This is a string of character" // read it =P
"This is a string of character"
> 10 + 32 // this is a sum of two numbers
42
> 0.33 * 3 // we also can do more advanced math
0.99
> 1 / 3 // but things might not be intuitive for you
0

The language even allows you to sum strings!

> "Abs" + "olute"
"Absolute"

Check the documentation [Language reference > Operator and expressions] to learn more about the possible expressions.

Those value can be stored in named boxes called variables. And after storing it, the content can be retrived by using its name.
A variable can be created with the var keywork and get its value extracted as follows:

> var foo = 10; // creates a box named foo containing 10

> var bar = 32 /* we need ; because the = is an statement */ ;

> foo + bar
42
> // notice that statements do not display a value after the > ... [return]

After the variable is created, it's value can be altered using the =, but a new variable with the same name can't be created in the same scope (more in the next post).

> var name = "Elisa";
> name + " Uhura"
"Elisa Uhura"
> name = "John"
"John"
> name + " Doe"
"John Doe"
> var name = "John";
// Not an error in the REPL, but a error in common code
> var a = 10; var a = "name";
(1,17): error CS0102: The type 'Submission#1' already contains a definition for 'a'

Wrapping up

With this basic knowledge, we are going to extend our initial program into a greeter by write the following in the REPL:

> var name = Console.ReadLine(); // type your name and press return
YOURNAME

> Console.WriteLine("Hello, " + name + "!");
Hello, YOURNAME!

This bit gives a hint about the next topic to be convered. See ya!


Go home.

☆Powered by Azure Blob