Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Write in .Net Core

I start to learn .Net Core. I want to write a simple 'Hello World' console application.

Unfortunately the System.Console is not available initially. This is my code:

using System;

class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello from Mac");        
    }
}

What package should I install?

FYI, I'm using Mac with VSCode and .net core rc1 update 2.

like image 856
mehrandvd Avatar asked Apr 09 '16 12:04

mehrandvd


People also ask

How do you write a console message in C#?

In C# you can write or print to console using Console. WriteLine() or Console. Write(), basically both methods are used to print output of console.

How do I open the console in .NET core?

You can select the Profile and change the Launch type to Project which allow the application to run in the console mode . That's it and you can choose the profile Name and run the project and it will run as console .

What is the use of system console write () method?

It resolves calls to System.Console.Write that include a string and an object array as a call to Write (String, Object). Formatting Types in .NET

How do I create a console application?

On the Create a new project page, enter console in the search box. Next, choose C# or Visual Basic from the language list, and then choose All platforms from the platform list. Choose the Console Application template, and then choose Next. If you don't see the .NET templates, you're probably missing the required workload.

What is NET Core CLI and how to use it?

.NET Core CLI provides different commands to create new project, build, clean and all other commands that we normally invoke using Visual Studio. There is a complete list of commands documented on the official documentation page and all the commands can be seen at this link.

How to build a net core application without IDE or Visual Studio?

We will see how we can build .NET Core based applications without using any IDE or Visual Studio. We will be using Command Line to create, build, and run the application. If we don’t need all the fancy features that Visual Studio and Visual Studio Code offer, then we can build a .NET Core application with just a Notepad.


Video Answer


3 Answers

Also, just to save someone else the minor headache: Don't make the mistake of naming your project "MyThing.Console" like I did, or the Console reference in your code won't be referencing System.Console, it will be referencing your namespace looking for a type called WriteLine!

like image 99
Dusty Avatar answered Oct 24 '22 02:10

Dusty


Just add NuGet Package

System.Console

to your project. No need to muss around with project.json. That way, you also get the latest (stable) version.

One gotcha: if you name your console project Something.Console, be sure to fully qualify the path to Write, i.e.

System.Console.Write();

like image 31
MonteChristo Avatar answered Oct 24 '22 02:10

MonteChristo


Make sure in your project.json system.console is referenced under frameworks:dnxcore50:dependencies

Example project.json:

{
  "version": "1.0.0-*",
  "description": "ConsoleApp1 Console Application",
  "authors": [ "danny" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
       "emitEntryPoint": true
  },

  "dependencies": {
  },

  "commands": {
    "ConsoleApp1": "ConsoleApp1"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Console": "4.0.0-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516"
      }
    }
  }
}
like image 11
Danny van der Kraan Avatar answered Oct 24 '22 02:10

Danny van der Kraan