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.
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.
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 .
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
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.
.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.
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.
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
!
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();
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"
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With