Hello C# 7.3 and .Net Core 2.1

First, we would like to say welcome everyone who read our first post on Brilliant Coder Zone web site. You can ask yourself why we say the web site, why we don’t call this web site just the blog? Well, the blogs are usually limited. We want that our web site evaluates in the future, but it will take some time that you will be able to see difference. You just need to know that this web site is a new one, active and in progress full time. So, you will be able to come back here always and follow our development devoted to you, exclusively in order that you have better and easier learn to coding.

You can also ask yourself why we chose first C# programming language of all others? Well, C# programming language is one of the best programming languages and when you once learned its structure, you will find it easier to learn other programming languages. Also, we chose to learn you C# programming language in Visual Studio Code source editor using Linux environment. We are focused more on the code than the design. It will be a magnificent challenge. Anyway, before we start we would like that you install:

OS: Linux

IDE: Visual Studio Code

Framework: .Net Core

Yes, we know if you use macOS you do not need install any Linux distribution and you can install Visual Studio Code on Windows, too. So, that is just your choice. Visual Studio Code is a source editor for Linux, macOS and Windows. You can use this great source editor for many programming languages too. Anyway, if you are one of Windows users, can you install Linux on your machine too? It could be a very interesting challenge. Brilliant Coders like Bash terminal more than Command Prompt. Yeah, we like to type. So, we will use Linux environment and we will not explain everything for all platforms. You will need to adapt to us. And there is a very seriously question. How to install Visual Studio Code and .Net Core Framework?

If you wish to do it on Windows it is easy like as any software you install on Windows. If you wish to do it on macOS then the installation is similar to the installation for Linux. And how to install Visual Studio Code and .Net Core to Linux and how to set Visual Studio Code to use C# programming language, you can watch on the video:

Now, if you ready with the necessary installations, we can begin to learn coding.

Open Visual Studio Code.

File -> Open Folder…

Let’s create a folder, call it hello_world wherever you wish on your hard disk, select it and open it. Look at on the bottom panel on the right side of VSC and click on the terminal tab. First, you need to check are you install everything all right. First, type the command:

dotnet

If you do not get this text below than you did not install everything all right and your VSC will not work at all.

Usage: dotnet [options]

Usage: dotnet [path-to-application]

Options:

-h|–help         Display help.

–info            Display .NET Core information.

–list-sdks       Display the installed SDKs.

–list-runtimes   Display the installed runtimes.

path-to-application:

The path to an application .dll file to execute.

You will need repeat all installations again. But if everything is all right you can continue. Let’s type a new command.

dotnet new console

And you will see that the command made you some files and put it in your folder. Let’s click on the Program.cs file in the explore panel on the left side of VSC. Now, you will see some C# code already written in the file Program.cs.

using System;

namespace hello_world

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Hello World!”);

}

}

}

As you can see you already have an auto-coded program Hello World. Let’s start C# code. Type the command:

dotnet run

And what happened? You got it:

Hello World!

Congratulations! You coded your first program without code at all. But you also didn’t learn to code anything. I hope you understand without coding you will learn nothing. So, let’s comment all code from Program.cs. What?

The first thing about C# programming language is that you know that C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft Corporation. C# programming language is based on C++ programming language. It is a case-sensitive programming language what means for example “Hello” and “hello” are two different words because “H” is uppercase in the first example and lowercase in the second example. You need to be careful when you write your code about this rule to avoid mistakes in your code.

The second, there are two kind comments in C# programming language and one XML documentation comment what you can use in your code.

  • Single Line Comments are used to comment a single line.

// Single Line Comment

  • Multiple Line Comments are used to comment more than one line.

/* Multiline

Comment */

  • XML Documentation Comments are special type of comment in C# programming language which used to create the documentation of C# programming language code by adding XML elements in the source code.

/// <summary>

/// XML Documentation Comment

/// </summary>

So, let’s comment with the multi-line comment auto-code in Program.cs file.

Select the code you want to comment.

Edit -> Toggle Block Comment

/* using System;

namespace helloworld

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Hello World!”);

}

}

} */

That you would not be confused with many details, you should know simply that every program for execution must have a special function Main. That’s the first thing that will start when you execute the program. But you cannot write functions outside of the class. So, we first need to make the class who will have Main function. Remember that C# programming language use open and close braces { } for blocks and that every command line must be finished with semicolon ;

class Program

{

static void Main(string[] args)

{

}

}

If you start this code above you will not get any result because there is not commands in Main function to do anything but you will also not get any error. If you get “Hello World” when you start your program, it means that you didn’t save your program. You can go to

Edit -> Save All

or better

Edit -> Auto Save

Now, you will not need to save your program every time when you change your code. Let’s say to print “Hello C# 7.3 and .Net Core 2.1”.

class Program

{

static void Main(string[] args)

{

System.Console.WriteLine(“Hello C# 7.3 and .Net Core 2.1”);

}

}

If you start this code above you will get this:

Hello C# 7.3 and .Net 2.1

That’s OK that your program works nice but sometimes you will have needs that you use your program with some other program. Because it, every program needs to have a namespace declaration. It helps that we know where is the program that we can call one program into another. We can say that the namespace is collection of classes. Every program has a namespace, usually the same name like as the program. Sometimes, the name of namespace can be the name of some company or both. For example, Company.Program We will call the namespace like as the program, it is hello_world.

namespace hello_world

{

class Program

{

static void Main(string[] args)

{

System.Console.WriteLine(“Hello C# 7.3 and .Net Core 2.1”);

}

}

But let’s see in the code above that our command to write the text is so long. Because it, there is the using keyword which used to include other namespaces in the program. We can write our command shorter if we include namespaces with using keyword.

using System;

namespace hello_world

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Hello C# 7.3 and .Net Core 2.1”);

}

}

}

Now if we change the code to add one more using keyword like as this:

using System;

using System.Console;

namespace hello_world

{

class Program

{

static void Main(string[] args)

{

WriteLine(“Hello C# 7.3 and .Net Core 2.1”);

}

}

}

It will not work. This code above will make an error. Why? Well, Console is not the namespace. For this case we can use static directive.

using System;

using static System.Console;

namespace hello_world

{

class Program

{

static void Main(string[] args)

{

WriteLine(“Hello C# 7.3 and .Net Core 2.1”);

}

}

}

How our program looks like, you can look at the video too.

 

Now, you try to change the text. Instead of “Hello C# 7.3 and Net 2.1”, write your full name.

Have a happy coding.

 

 

 

Leave a Reply

Close Menu