TOC

This article is currently in the process of being translated into Greek (~42% done).

Getting started:

Hello world explained

Στο προηγούμενο κεφάλαιο, προσπαθήσαμε να γράψουμε ένα μικρό κομμάτι κειμένου στην κονσόλα, δηλαδή στην πρώτη μας εφαρμογή γραμμένη σε C#. Είδαμε τι γίνεται όταν τρέξουμε αυτή την εφαρμογή, χωρίς όμως να μπούμε σε λεπτομέρεια σχετικά με τις γραμμές κώδικα που χρησιμοποιήσαμε, οπότε σε αυτό το κεφάλαιο θα εξηγήσουμε τον κώδικα για το παράδειγμα "Hello world". Όπως πιθανότητα μπορείτε να δείτε από τον κώδικα, μερικές από τις γραμμές του φαίνονται παρόμοιες, για αυτό θα τις επαναφέρουμε σε ομάδες και θα τις εξηγήσουμε ξεχωριστά. Ας ξεκινήσουμε από τους πιο σύντομους και συνηθισμένους χαρακτήρες του κώδικά μας: { and }. Συνήθως αναφέρονται ως αγκυστροειδείς αγκύλες και χρησιμοποιούνται σε πολλές άλλες γλώσσες προγραμματισμού, όπως C++, Java, JavaScript και άλλες πολλές. Όπως μπορείτε να δείτε στον κώδικα, χρησιμοποιούνται για να συμπεριλάβουν αρκετές γραμμές κώδικα οι οποίες πάνε μαζί. Σε επόμενα παραδείγματα, θα γίνει πιο ξεκάθαρο το πώς χρησιμοποιούνται.

Ξεκινώντας απ' την αρχή:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using είναι μία λέξη κλειδί, τονισμένη με μπλε χρώμα από το πρόγραμμα με το οποίο γίνεται η επεξεργασία του κώδικα (editor). Χρησιμοποιείται για να εισάγει ένα namespace, το οποίο είναι μία συλλογή από κλάσεις. Οι κλάσεις δίνουν κάποιο είδος λειτουργικότητας, και όταν δουλεύει κανείς με προχωρημένους IDE όπως είναι το Visual Studio, συνήθως δημιουργούν για εμάς τετρημμένα τμήματα κώδικα. Στο παράδειγμά μας, δημιούργησε μία κλάση για εμάς και εισήγαγε namespaces οι οποίοι απαιτούνται ή συνήθως αναμένεται να χρησιμοποιηθούν. Συγκεκριμένα βλέπουμε ότι πέντε (5) namespaces έχουν εισαχθεί για εμάς, καθένας απ' τους οποίους εμπεριέχει πολλές χρήσιμες κλάσεις. Για παράδειγμα, χρησιμοποιούμε την κλάση Console, η οποία είναι κομμάτι του System namespace.

Αντιθέτως, το namespace System.Linq δεν το χρησιμοποιήσαμε στο παράδειγμά μας, οπότε μπορεί να αφαιρεθεί αυτή η γραμμή κώδικα, χωρίς να επηρεάζει το συνολικό αποτέλεσμα σε αυτό το σημείο.

Όπως μπορείτε να δείτε, έχει δημιουργηθεί μέχρι και το δικό μας namespace:

namespace ConsoleApp1

The namespace ConsoleApp1 is now the main namespace for this application, and new classes will be a part of it by default. Obviously, you can change this, and create classes in another namespace. In that case, you will have to import this new namespace to use it in your application, with the using statement, like any other namespace.

Next, we define our class. Since C# is truly an Object Oriented language, every line of code that actually does something, is wrapped inside a class. In this case, the class is simply called Program:

class Program

We can have more classes, even in the same file. For now, we only need one class. A class can contain several variables, properties and methods, concepts we will go deeper into later on. For now, all you need to know is that our current class only contains one method and nothing else. It's declared like this:

static void Main(string[] args)

This line is probably the most complicated one in this example, so let's split it up a bit. The first word is static. The static keyword tells us that this method should be accesible without instantiating the class, but more about this in our chapter about classes.

The next keyword is void, and tells us what this method should return. For instance, it could be an integer or a string of text, but in this case, we don't want our method to return anything (C# uses the keyword void to express the concept of nothing).

The next word is Main, which is simply the name of our method. This method is the so-called entry-point of our application, that is, the first piece of code to be executed, and in our example, the only piece to be executed.

Now, after the name of a method, a set of arguments can be specified within a set of parentheses. In our example, our method takes only one argument, called args. The type of the argument is a string, or to be more precise, an array of strings, but more on that later. If you think about it, this makes perfect sense, since Windows applications can always be called with an optional set of arguments. These arguments will be passed as text strings to our main method.

And that's it. You should now have a basic understanding of our first C# application, as well as the basic principles of what makes a console application work.