2

Playing with VSCode: C# Hello World Project

 2 years ago
source link: https://vainolo.com/2020/05/03/playing-with-vscode-c-hello-world-project/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Playing with VSCode: C# Hello World Project

Published by vainolo on 2020-05-03

Visual Studio Code (or VSCode at it usually known) has become one of the most widely used IDEs, and not by mistake. The interface is simple and very fast. Although it doesn’t have all of the goodies that come with “bigger” IDEs (Eclipse, PyCharm, Visual Studio), it has many extensions that fill parts of this gap. You can download VSCode here.

The best way to get started with a new IDE is by creating a simple project, and as usual, it will be “hello world”. Let’s do this step by step. You will be needing the .NET core SDK, which you can download here (make sure to download the SDK and not the runtime). I’m using Windows 10, VSCode 1.44.2 and .NET core 3.1 for this project.

Let’s open up VScode. This is the screen that pops up:

To start, open a folder which will be the location where we write the program. I’ll put mine in c:\dev\VSCodeCSharpHelloWorld. The window changes a bit and we are now looking at the contents of the folder. We’ll be using the built-in terminal, so open it up by clicking Ctrl+` or through the menu “View->Terminal”. It should look something like this:

Unlike other IDEs, there is no built-in concept of a solution or project. There is something called a “Workspace” that allows us to work with multiple folders at a time, but that is for more advanced projects.

To create a new console application, navigate to the console, and write:

> dotnet new console

This creates a project file with the name of your directory, and a C# file called Program.cs that contains the code of the program. Now let’s run the program we have just created. Type in the console

> dotnet run

You should see the text “Hello World!” written in the terminal.

Now let’s change the program to do a bit more, asking for your name and then writing “Hello <Your Name>”. Open the Program.cs file by double-clicking it. Your editor should look like this:

Now insert a new line above Console.WriteLine and type Console.. At this step, you would expect some kind of code complete/suggest to kick-in. But no! Code complete/suggest does not come bundled into VSCode (that is one reason it is so lean), but it is easy to add. Go to the “Extensions” view by clicking on the fifth icon on the left (or  “View->Extensions”). You will be shown a long list of extensions that are available to install. For now, we just need the “C#” extension, so click on the “install” button right next to it.

After the extension is installed (no need to reopen the app, not to say reboot the computer!), navigate back to the “Program.cs” file, and write again Console.. Voila! The properties and methods of the class are shown. Continuing from where we left, add two lines to the main method, making it look like this:

static void Main(string[] args)
{
Console.WriteLine("What is your name?");
var name = Console.ReadLine();
Console.WriteLine($"Hello {name}!");
}

Enter dotnet run again in the terminal. The program will ask for your name, write it and hit enter:

Awesome!

Lately, I’m working almost every day with VSCode, in parallel to both PyCharm and Visual Studio Enterprise and I enjoy it a lot. I still have the other IDEs open sometimes because not all the functionality in them exists in VSCode but with each release (and they move quite fast) it is getting better and better, without getting slower and bulky. Great job VSCode team!

I have more ideas for VSCode tutorials and with some time I’ll get to them soon. If you have a good one, leave a comment and I’ll add it to my list.

Until next time, happy coding!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK