Code Monkey home page Code Monkey logo

dotnetcore's Introduction

DotnetCore

A quick boilerplate demo of .NET core

Table of content

  1. Prerequisites
  2. Creating a Console App

Prerequisites

  • Make sure you have installed the .NET Core SDK v 2.0+ Download here

Verify the you have everything needed by opening Powershell or another command console tool and type

dotnet --version

A version number greater than or equal to 2.0.0 should appear. If so you are all set!

Back to top

Creating a Console App

  • Create a new project file (filename.csproj)

Add the following configuration in your project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

</Project>
  • Once saved try running the dotnet run command from within the root of your new project folder. An error will occur!

Notice: The dotnet run command will build a debug version of your code. If you need to run a release version, you should run

dotnet run -c Release

When running the command, you will get the following error..

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [*******.csproj]

The build failed. Please fix the build errors and run again.
  • Every Exe (executables) in .NET needs an entry point. Let's make a file program.cs and add the entry point The main method
using System;

namespace firstconsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello from xemmel's .NET Core tutorial!");
        }
    }
}
  • Run the dotnet run command again, and this time you should have a nice message appearing in your console before the program terminates. Notice that it does take some time for the program to start running.

Spice it up

Most of the basic utitilies you are used to use in the normal .NET Framework can also be used directly in .NET Core without the need for adding any (nuget) packages. The following example uses both extensions and LINQ

using System;
using System.Linq;

namespace firstconsole
{
   class Program
   {
       static void Main(string[] args)
       {
           string message = null;
           if (args.Length == 0) {
               message = "Hello from xemmel's .NET Core tutorial!";
           }
           else {
               message = args.First();
           }
           Console.WriteLine();
           Console.WriteLine($"The message:\r\n{message}\r\nContains {message.WordCount()} words");
           Console.WriteLine();
       }
   }

   public static class Extensions
   {
       public static int WordCount(this string message)
       {
           if (string.IsNullOrWhiteSpace(message))
               return 0;
           string[] words = message.Split(' ');
           return words.Count();
       }
   }
}

try running the program with or without your own message argument

PS C:\> dotnet run "I want to learn more Core"

The message:
I want to learn more Core
Contains 6 words

Back to top

dotnetcore's People

Contributors

xemmel avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.