Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Write Your First .NET Core 2.0 Application

DZone's Guide to

How to Write Your First .NET Core 2.0 Application

In this post, you'll get a quick introduction to creating web applications in C# using Microsoft's .NET Core 2.0. Let's get to it!

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

Microsoft .NET Core is a cross-platform open-source software development framework that can be used to build applications for mobile, Windows, and the web. You can learn more about .NET Core here, but in this blog post, we'll walk you through how to create and publish a .NET Core application for Windows.

To work with .NET Core, first you need to install it from here. While you can use any IDE to create a .NET Core application, I am going to use the Visual Studio 2017 Enterprise version. If you do not have Visual Studio installed, you may want to try the community edition, which can be found for free here. Once the environment is set, launch Visual Studio and create a new project by selecting File->New Project-> Visual C#-> .NET Core-> Console App. Besides C#, a .NET Core application can be used in other languages like Visual Basic.

After successfully creating your project, you will see there is already template code in the Program.cs file which looks like the code block shown below:

Code Block 1
using System;
namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

This is the bare minimum amount of code for a .NET Core application, which will print Hello World as the output. To build a project and run the application press F5. You will get the output shown in the below image.

Now, let's modify the code to find the summation of two numbers. You can read the input from the keyword and add two numbers as shown in the code block, below:

Code Block 2
using System;

namespace helloworld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("My 1st .NET Core application");
            Console.WriteLine("Enter Number 1");
            double num1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter Number 2");
            double num2 = Convert.ToDouble(Console.ReadLine());
            double result = num1 + num2;
            Console.WriteLine(num1 + " + " + num2 + " = " + result);
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);
        }
    }
}

To build this project and run the application press F5. You will get the output shown in the next image:

So far, we have built a basic .NET Core application. And we can also publish this .NET Core application right from Visual Studio. To do that, right click project in the solution explorer and click on Publish.

Here you can publish your app to Azure or another host. Right now, I am publishing it to a local folder, as you can see in the next image:

To publish, click on the publish button. In the Output window, you can see the publish success message as shown in the image below:

Now when you open the folder where you published your app, you will find various files. You need to run the .dll file to run the .NET Core application:

You can run your new .NET Core application using the command dotnetrun. Therefore, to run this application, execute the command dotnetnet helloworld.dll as shown in the image below:

And as you can see, your new Windows application is published and running!

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
web dev ,.net core ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}