Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development

You're reading from   C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code

Arrow left icon
Product type Paperback
Published in Oct 2019
Publisher Packt
ISBN-13 9781788478120
Length 818 pages
Edition 4th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mark J. Price Mark J. Price
Author Profile Icon Mark J. Price
Mark J. Price
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

1. Hello, C#! Welcome, .NET! FREE CHAPTER 2. Speaking C# 3. Controlling Flow and Converting Types 4. Writing, Debugging, and Testing Functions 5. Building Your Own Types with Object-Oriented Programming 6. Implementing Interfaces and Inheriting Classes 7. Understanding and Packaging .NET Types 8. Working with Common .NET Types 9. Working with Files, Streams, and Serialization 10. Protecting Your Data and Applications 11. Working with Databases Using Entity Framework Core 12. Querying and Manipulating Data Using LINQ 13. Improving Performance and Scalability Using Multitasking 14. Practical Applications of C# and .NET 15. Building Websites Using ASP.NET Core Razor Pages 16. Building Websites Using the Model-View-Controller Pattern 17. Building Websites Using a Content Management System 18. Building and Consuming Web Services 19. Building Intelligent Apps Using Machine Learning 20. Building Windows Desktop Apps 21. Building Cross-Platform Mobile Apps Using Xamarin.Forms

Building console apps using Visual Studio Code

The goal of this section is to showcase how to build a console app using Visual Studio Code. Both instructions and screenshots in this section are for macOS, but the same actions will work with Visual Studio Code on Windows and Linux variants.

The main differences will be native command-line actions such as deleting a file: both the command and the path are likely to be different on Windows or macOS and Linux. Luckily, the dotnet command-line tool will be identical on all platforms.

Writing code using Visual Studio Code

Let's get started writing code!

  1. Start Visual Studio Code.
  2. On macOS, navigate to File | Open..., or press Cmd + O. On Windows, navigate to File | Open Folder…, or press Ctrl + K Ctrl + O. On both OSes, you can click the Open Folder button in the EXPLORER pane or click the Open Folder… link on the Welcome page, as shown in the following screenshot:
  3. In the dialog box, navigate to your user folder on macOS (mine is named markjprice), your Documents folder on Windows, or any directory or drive in which you want to save your projects.
  4. Click the New Folder button and name the folder Code.
  5. In the Code folder, create a new folder named Chapter01.
  6. In the Chapter01 folder, create a new folder named HelloCS.
  7. Select the HelloCS folder and on macOS click Open or on Windows click Select Folder.
  8. Navigate to View | Terminal, or on macOS press Ctrl + ` (backtick) and on Windows press Ctrl + ' (single quote). Confusingly on Windows the key combination Ctrl + ` (backtick) splits the current window!
  9. In TERMINAL, enter the following command:
    dotnet new console
  10. You will see that the dotnet command-line tool creates a new Console Application project for you in the current folder, and the EXPLORER window shows the two files created, HelloCS.proj and Program.cs, as shown in the following screenshot:
  11. In EXPLORER, click on the file named Program.cs to open it in the editor window. The first time that you do this, Visual Studio Code may have to download and install C# dependencies like OmniSharp, the Razor Language Server, and the .NET Core debugger, if it did not do this when you installed the C# extension.
  12. If you see a warning saying that required assets are missing, click Yes, as shown in the following screenshot:
  13. After a few seconds, a folder named .vscode will appear in the EXPLORER pane. These are used during debugging, as you will learn in Chapter 4, Writing, Debugging, and Testing Functions.
  14. In Program.cs, modify line 9 so that the text that is being written to the console says, Hello, C#!
  15. Navigate to File | Auto Save. This toggle will save the annoyance of remembering to save before rebuilding your application each time.

Compiling and running code using dotnet CLI

The next task is to compile and run the code.

  1. Navigate to View | Terminal and enter the following command:
    dotnet run
  2. The output in the TERMINAL window will show the result of running your application, as shown in the following screenshot:
You have been reading a chapter from
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development - Fourth Edition
Published in: Oct 2019
Publisher: Packt
ISBN-13: 9781788478120
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime