[Part 1 - Hello World] Learning Server-side C# with DotNetCore in MacOS

Published Feb 04, 2017

So you want to learn C#? you want it to be your friend when scripting in the server? or even making services? or even user apps!

Well this post is for you!


So what is DOTNETCORE?

.NET Core is a general purpose development platform maintained by Microsoft and the .NET community on GitHub. It is cross-platform, supporting Windows, macOS and Linux, and can be used in device, cloud, and embedded/IoT scenarios.

So basically DOTNETCORE allows you to run and execute C# code everywhere!

Lets get started!

First of all we need to install DOTNETCORE if you haven't already.

Step 1: Install Homebrew for MacOS: http://brew.sh/

The missing package manager for macOS

It basically allows us to install packages and software required for this tutorial easily.

Step 2: Once installed you should be able to type brew -v in the terminal
You should see something similar to this

0_1484270669891_Screen Shot 2017-01-13 at 9.23.51 AM.png

Step 3: DOTNETCORE requires OpenSSL so lets go ahead and install that as well. And you guessed it we can install it from homebrew!

OpenSSL is an open source project that provides a robust, commercial-grade, and full-featured toolkit for the Transport Layer Security (TLS) and Secure Sockets Layer (SSL) protocols. It is also a general-purpose cryptography library. For more information about the team and community around the project, or to start making your own contributions, start with the community page. To get the latest news, download the source, and so on, please see the sidebar or the buttons at the top of every page.

go ahead and type brew install openssl

Once that finishes, execute the following commands to put OpenSSL to proper folder required by DOTNETCORE

mkdir -p /usr/local/lib
ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

Step 4: Finally lets download the actual DOTNETCORE Installer and well... Install it!

Now lets verify that you have DOTNETCORE Installed correctly. you can do that by typing "dotnet --version" in the terminal.

and you should see something like this. (version number may vary)
0_1484271169699_Screen Shot 2017-01-13 at 9.32.25 AM.png

If you see something else then the install process must've been wrong. Please check back the guide and come back here when its working.

Great now we can actually start!


Now lets start by creating a folder for our C#/DOTNETCORE project

for now lets create a folder called dotnet_projects, we can do that by typing

mkdir ~/dotnet_projects

once the folder is created lets go ahead and go inside that folder by typing

cd ~/dotnet_projects

finally DOTNETCORE provides an easy way to create a project template for us. We can do that by typing

dotnet new

and it should say something like this:
Created new C# project in ~/dotnet_projects.

Now if you type ls command which lists the files and sub folders in our current project folder, you should be able to see something like this

0_1484271645907_Screen Shot 2017-01-13 at 9.40.38 AM.png

Great! now we have a project template set up, lets get editing!

Now open your favorite text editor open the Program.cs file that we just generated and paste this:

using System;

class Program
        static void Main(string[] args)
                Console.WriteLine("Hello C# DOTNETCORE in MacOS!");

And that's it! Your first few lines of C#, Save the file!

We will explain more details about this code later on. But for now this line should be able to display text in the console with the message "Hello C# DOTNETCORE in MacOS!".

But hey! why don't we just run code to check?


Now open terminal (again if you closed it 😛) and navigate to the project folder.
( cd ~/dotnet_projects )

Before we can run, we need first to restore any libraries that our project requires. In this case its not that much besides the default DOTNETCORE libraries.

Type dotnet restore

0_1484271928226_Screen Shot 2017-01-13 at 9.45.21 AM.png

Now go ahead and run the code by typing: dotnet run

and you should see something like this
0_1484272031431_Screen Shot 2017-01-13 at 9.46.55 AM.png

Congratulations! You have coded and executed your first C# code!

In the next episode we will dive deeper into the C# and DOTNETCORE world and find out how the code we just wrote works and also explore the basics of C# syntax.

Thanks for reading!

Discover and read more posts from Clark Alesna
get started