Category Archives: .Net Core

C#: Hello World

Let’s write some actual code today and there are few places better to start than with the traditional ‘Hello World’ program.

For this post, I want to give an introduction to writing our first functional C# program. It will not do much but hopefully, for those new to software development it will be interesting.

We’ll develop the app by using PowerShell (or command line but some of the commands will be different) and a simple text editor (Notepad) but if you want to you can follow through in Visual Studio, I’ve put a setup guide together for Visual Studio 2017 if you need/want it.

The first thing we’ll do is check we have dotnet installed. Open PowerShell and run the command

dotnet --version

You should see something along the lines of the following:

dotnet version

If you don’t see this or you’re told dotnet isn’t installed, check out this guide to install it.

Great, so dotnet is installed and we can now start writing our code.

We’ll want to go ahead and create our project directory and change into it.

mkdir hello_world
Make project directory

Now that we have our project directory we can create the project itself. We are going to be making a console app and all we want it to do is print ‘Hello World!’ to the console. The easiest way to create the app is from the command line, as we are there already. There are a few options available to us, and not just for console apps, but I’ll put a post together about that another time. For now, we just want a console app created with the following command.

dotnet new console
dotnet new console

We should now have two files inside our hello_world directory, a Program.cs file and a hello_world.csproj file.

new project contents

The Program.cs file will contain our code, it currently has the default codebase provided by dotnet for a console app, shown below in the red box.

Some of you may notice that it appears this app does what we want it to already. We’ll make some changes so we can introduce some other components in software development.

The hello_world.csproj file is our project file, this is an MSBuild project file for a C# project. It contains information about the project and how to build it. For the purposes of this post, its not something we need to worry about just that we need the default.

Let’s modify our Program.cs file to provide us with a bit more to work with, to open the Program.cs file in a text editor (on Windows) run the following command in the hello_world project directory.

Notepad Program.cs
Open Program.cs in Notepad

This will open the Program.cs file in Notepad as shown below.

Program.cs open in Notepad

Great stuff, now let’s make some changes. Update your Program.cs file inline with the code below.

using System;    // 1. Importing namespace

namespace hello_world    // 2. Hello World namespace
{
    public class Program    // 3. Class declaration
    {
        static void Main(string[] args) // 4. Method declaration
        {
            string helloWorldMessage = "Hello World!"; // 5. Statement 1
            Console.WriteLine(helloWorldMessage); // 6. Statement 2
        } // End of Method
    } // End of Class
} // End of namespace

Now that we have some code to work with let’s go through it.

Firstly, I’d like to explain what ‘//’ means, everything on the line after the ‘//’ is called a comment and is ignored by the app. That means it doesn’t matter what we put there the compiler isn’t interested in it. The text (in grey) is for humans only.

Now let’s start going over the code line by line.

using System;

This line allows us to reduce our codebase, by including it we don’t have to specify the fully qualified domain name to access the contents of the System namespace. See the below example

// Without 'using System;' we'd have to use the fully qualified domain name to use WriteLine
System.Console.WriteLine(helloWorldMessage);

// But because we did include 'using System;' at the beginning we can omit it and still access WriteLine
Console.WriteLine(helloWorldMessage);

Following that, we have our namespace declaration.

namespace hello_world
{
... // Rest of the code.
}

The first line declares the name of the namespace which is hello_world, think of a namespace as a container so we can organise our code. Everything between the opening { and closing } is contained within the scope of the namespace and so is part of it.

NOTE: Any code caught between ‘{‘ and ‘}’ is said to be in the scope of whatever created that scope; namespaces, classes, structs, methods, loops etc.

Next up we have our class definition:

public class Program
{ // No need to talk about scopes again
... 
}

To define a class with follow the class definition syntax

// [access modifier] - [class] - [identifier]
public class Program

Our access modifier is public, there are several access modifiers in C# but for now public means this class can be accessed by any other code either in the assembly or any other assembly that references it. We can skip adding the access modifier and the compiler will then use the default internalwhich allows only code in the same assembly to use it.

NOTE: If you omit the access modifier anywhere in C# the complier will plug in the most restricted access that can be declared for that member.

As we are creating a class we use the class keyword to define it as such.

Finally, we provide the identifier for the class, that is we provide the name and for us that’s Program.

Next, we define the first and only method within our Program class, called Main

As with a class definition we have a set method definition syntax we can follow.

// [access modifier] - [optional modifier] - [return type] - [Identifier] (Parameter List)
static void Main(string[] args)

Main‘ is important as it is the entry point for our program, this is where the app starts from as far as we are concerned. Interesting because the ‘Main‘ method is marked as the entry point in IL, see this post for more information on how .Net works, it doesn’t have to be marked with an access modifier, i.e. made public, because behind the scenes the CLR knows where to start from.

First up then we have the static keyword which is an optional modifier. Because Main is our entry point we don’t need to worry about static for now but I’ll link to the relevant post explaining static in detail in the future. For now, if you’ve read my OO post static associates the method with the class and not with an object created from the class.

Next is our return type, by default, we don’t return anything from
Main so we use the keyword void to identify it as such however this can be any data type. Traditionally the type has been returned by value but as of C# 7.0 it can not be returned by reference, again not something to worry about now, I’ll do a post explaining value and reference types.

Following our return type, we have the identifier, again this is the name. Quite straight forward this one, the name is ‘Main‘.

Finally, we have our parameter list, this is simply a list of 0 or more types and names required by the method and the corresponding arguments provided by the client. In this case, it would be any arguments passed in when starting the app.

NOTE: To confirm and avoid any confusion in the future, a method defines what parameters are required and a caller of the method provides arguments for each parameter.

Now that we have our method defined we can move onto our first (and only) statement block, this contains the statements (actions) which are performed sequentially within the scope of the method.

We have two statements, the first:

string helloWorldMessage = "Hello World!";

This is a declaration statement, we are declaring a new local variable of type string and naming it helloWorldMessage. We use an expression to assign the value “Hello World!” to our new variable using the assignment operator ‘=‘. To access the value of the string we then use helloWorldMessage, as shown in the next line.

A variable is a storage location which can contain different values over time but not different types, this is because C# is a strongly-typed language. We must know what each storage location will contain at compile time. So each variable is of a specific type and the type defines the characteristics of the storage location. In our case, we have a string type which means we can only put text (a sequence of read-only characters) into our storage location.

Our next statement is:

Console.WriteLine(helloWorldMessage);

Here we are using a method provided by .Net Core found in the System namespace. The method itself is static, provides no return type, and is in the Console class. There are multiple versions of the WriteLine method each having a different collection of parameters. For us, the parameter is a string, as we are passing in our string contained in helloWorldMessage, so we are using the following fully qualified Console.WriteLine

System.Console.WriteLine(string value);

This method, as described by the Microsoft documentation:

Writes the specified string value, followed by the current line terminator, to the standard output stream.

The remaining content in the file is scope closing ‘}”s described above.

Ok, so we’ve written our program and we’ve worked through it and have an understanding of what each part does. Now let’s run it.

In your terminal, make sure you’re inside the project directory, run the following:

dotnet run

This will build our code using the dotnet build command and then run it for us. You should see something shown below.

dotnet run

Congratulations. Doesn’t do much and has no real value except to us but a ‘Hello World’ app is the starting point for almost all developers.

I’ll add a post providing C# keywords.

For now, why not play around with it and have it print different things to the screen. For those, a bit more curious you might’ve noticed my text is a different colour to yours, have a go and find out how you can change the text colour from inside your app.

Update: Code can be found here.

.Net: C#

Ok, so I’ve talked about the .Net Framework and .Net Core now I think it’s time we have a bit of an introduction to a .Net language, specifically C# (pronounced ‘C Sharp’).

C# is a member of the C family of languages, this includes C, C++, Java, Objective-C, and many others. It is commonly referred to as a general-purpose, type-safe, object-oriented programming language. Let’s quickly break this down:

  • General-Purpose: Means the language has been designed for application in many different domains.
  • Type-Safe: Means behaviours within our programs are well defined.
  • Object-oriented: Means we create our programs around the concept of real-world objects. See this post.

The core syntax for C# is very similar to that of Java but it is not a clone, at least not directly. C# was primarily modelled after Visual Basic (another .Net Language) and C++ (developed by Bjarne Stroustrup.

C#, however, gives us a lot of what other languages do, including overloading, overriding, structs, enums, and delegates (callbacks), but also gives use features more commonly found in functional languages, like F# and LISP, such as anonymous types and lambda expressions. All of which I will be writing about in the future.

The intention of the language was to be a simple yet modern language which provides portability and deployment to different types of environments. With the release of .Net Core, I’d say it is certainly living up to expectations.

The latest stable release of C# is currently at 7.3 as of May 2018 and the latest Preview release is at 8.0. Keep an eye out for a post that gives an overview of the changes provided to us at each release.

I won’t go into too much detail for this post as I want to start writing smaller posts targeting specific parts of the C# language.

.Net: Core

So we’ve looked at the .Net Framework which was limited to Windows, now lets check out .Net Core…

.Net Core was announced by Microsoft in June, 2016 and showed a new path they were creating the .Net ecosystem, and while this new direction was based on C# and the .Net Framework it was quite different.

Two of the biggest changes for the .Net ecosystem was it would be cross-platform and released across Windows, macOS, and Linux at the same time, in contrast to the .Net Framework only ever having been available on Windows. The second change was that .Net Core was going to be Open-Source, that is COMPLETELY Open-Source, meaning the code is available for us to review so we can get a better understanding of what is actually happening. But also the community is encouraged to actually get involved by way of pull requests and the like. This means we actually get our eyes on the Framework and documentation, raise issues such as bug reports, fix bugs, recommend or implement improvements, and add additional functionality. Not bad right?

The origin of .Net Core came from developers requesting that ASP.NET ran on multiple platforms and not just Windows. From there we got ASP.NET 5 and Entity Framework 7, which to be honest was more than a little confusing considering we had .Net Framework 4.6 and Entity Framework 6 you’d expect them to be the next releases right? Not so, so we got .Net Core, ASP.NET Core, and Entity Framework Core to show the distinction between the two ecosystems.

Since the announcement in 2016, we’ve seen the release of .Net Core 1.0, .Net Core 1.1 released in March 2017, .Net Core 2.0 released in August 2017, and .Net Core announced in May 2018 and we’re currently at .Net Core 3 Preview 3 as of March 2019. So we’re making some decent progress and getting more and more functionality with each release.
But what is driving these releases? Microsoft is developing their .Net offering (Core and Framework) inline with a formal specification call the .Net Standard which we are currently at version 2.0. The goal is to have the whole .Net ecosystem establish uniformity. The image below is directly from Microsoft and as they say “a picture paints a thousand words”, which I think makes their goal a bit more obvious.

I haven’t touched on Xamarin because I want to save that for its own post. Check out this post for a very quick intro though.

From the image and the statement above their goal is to have a single library which can be used for the .Net Framework, .Net Core, and Xamarin.

By use of the NuGet Package Manager we can download .Net Standard or third party created packages which contain just the constructs or functionality we need, greatly reducing our production footprint. An example is if you need a JSON parser you could swing by the NuGet Package Manager and add an implementation of your choice, such as JSON.Net to your project. This is a very easy and very quick way for us to develop and we don’t have to find implementations out on the web that may or may not do what we need.

In addition we have also been given Command Line Support, that is we can create, develop, build, test, and publish our applications all from the command line, again all cross-platform. Certainly worth looking into further so keep an eye out for that post if it interests you.
We also get the Universal Windows Platform (UWP), which you can see in the image above, which follows Microsofts write once run anywhere approach. By using UWP we can run the same code on multiple Windows platforms including IOT device (like Raspberry Pi), Mobiles Device running Windows 10 Mobile, PC, Hololens, Surface Devices, and even on Xbox One.

If you checked out the Xamarin post I mentioned above you’ll know Xamarin is a cross-platform development framework, so if we combined our development activities between .Net Core and Xamarin we can get both a huge amount of code reuse and a massive market penetration.

So to round of the .Net Core discussion, we can get a huge amount of code reuse through cross-platform support, we get more visibility of what the framework is doing and increased confidence through community involvement with it being Open-Source, we only use what we need resulting in smaller binaries and dependencies and increased performance, and we have full command line support. Quite exciting…

I hope you have found this post informative or at the very least interesting. I’m looking forward to writing up more .Net related posts in the future.