mattak / ConsoleSample

C# Console Application Example working on Mac.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ConsoleSample

C# project sample for non windows environment. Here is instructions to create simple console application working on Mac.

Workflow: Create Console Application

Donwload & install dotnet tool chain

Create project folder.

$ mkdir ConsoleSample
$ cd ConsoleSample

Create Solution file.

$ dotnet new sln

Create Application project.

$ mkdir App
$ cd App
$ dotnet new console
$ cd ..

Now, we get a following directory tree.

$ tree
.
├── App
│   ├── App.csproj
│   └── Program.cs
└── ConsoleSample.sln

Following file was created by dotnet new console

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Add App project to Solution file.

$ dotnet sln ConsoleSample.sln add App/App.csproj

Restore & Build

$ dotnet restore
$ dotnet build

Run

$ dotnet run --project App/App.csproj
Hello World!

Workflow: Add Library

Now, we'll try to add Json.NET.

$ dotnet add App/App.csproj package Newtonsoft.Json

Revise program to use JSON library.

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = new Dictionary<string, string>() {{"message", "Hello World!"}};
            var serialized = JsonConvert.SerializeObject(dictionary);
            Console.WriteLine(serialized);
        }
    }
}

Run

$ dotnet run --project App/App.csproj
{"message":"Hello World!"}

About

C# Console Application Example working on Mac.


Languages

Language:C# 100.0%