{"id":42,"date":"2021-12-21T09:26:58","date_gmt":"2021-12-21T02:26:58","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=42"},"modified":"2023-03-05T21:55:38","modified_gmt":"2023-03-05T14:55:38","slug":"csharp-hello-world","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-hello-world\/","title":{"rendered":"C# Hello World"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create a simple program that displays the famous message Hello, World! on a console window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the C# hello world program<\/h2>\n\n\n\n<p>First, launch Visual Studio.<\/p>\n\n\n\n<p>Next, click the <strong>Create a New Project<\/strong> button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-1-1024x719.png\" alt=\"\" class=\"wp-image-47\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-1-1024x719.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-1-300x211.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-1-768x539.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-1.png 1334w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then, select the Console App to create a project for a command-line application that runs on .NET core on Windows, Linux, and macOS, and click the Next button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-2-1024x719.png\" alt=\"\" class=\"wp-image-48\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-2-1024x719.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-2-300x211.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-2-768x539.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-2.png 1334w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After that, configure the new project by entering the project name, location, and solution name, and click the Next button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-3-1024x719.png\" alt=\"\" class=\"wp-image-49\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-3-1024x719.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-3-300x211.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-3-768x539.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-3.png 1334w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Finally, select the .NET framework, 6.0 (Long-term support) in this case and click the Create button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"719\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-4-1024x719.png\" alt=\"\" class=\"wp-image-50\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-4-1024x719.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-4-300x211.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-4-768x539.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-4.png 1334w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The Visual Studio will show up as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"544\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5-1024x544.png\" alt=\"\" class=\"wp-image-52\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5-1024x544.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5-300x159.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5-768x408.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5-1536x816.png 1536w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-5.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>On the left-hand side is the code editor where you can enter the C# code. By default, Visual Studio automatically generates a statement that shows the <code>Hello, World!<\/code> message.<\/p>\n\n\n\n<p>Also, Visual Studio shows the solution explorer on the right pane. A solution may contain one or more projects. In this example, the <code>HelloWorld<\/code> solution has one project with the same name. <\/p>\n\n\n\n<p>Under the <code>HelloWorld<\/code> project is the source code file <code>program.cs<\/code>. Note that all C# source code files have the extension <code>.cs<\/code> that stands for CSharp.<\/p>\n\n\n\n<p>The following shows the contents of the program.cs file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ See https:\/\/aka.ms\/new-console-template for more information<\/span>\nConsole.WriteLine(<span class=\"hljs-string\">\"Hello, World!\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this program:<\/p>\n\n\n\n<p>The first line that starts with double forward slashes <code>\/\/<\/code>  is a comment. It doesn&#8217;t have any effect on the program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ See https:\/\/aka.ms\/new-console-template for more information<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The second line writes the message <code>\"Hello, World!\"<\/code> to the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Console.WriteLine(<span class=\"hljs-string\">\"Hello, World!\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this code, the <code>Console.WriteLine<\/code> is a method that accepts a text as the input and writes that text to the console window.<\/p>\n\n\n\n<p>To run the program, you click the <code>HelloWorld<\/code> button or press the F5 keyboard. Behind the scene, VS will compile and run the program. The following console window will display the &#8220;Hello, World!&#8221; message:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"582\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-6-1024x582.png\" alt=\"\" class=\"wp-image-53\" srcset=\"https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-6-1024x582.png 1024w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-6-300x170.png 300w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-6-768x436.png 768w, https:\/\/www.csharptutorial.net\/wp-content\/uploads\/2021\/12\/csharp-hello-world-step-6.png 1348w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Congratulation! you have successfully created and run the Hello, World! program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the using static syntax<\/h2>\n\n\n\n<p>C# 6 allows you to shorten the code by using the &#8220;<code>using static<\/code>&#8221; syntax. So instead of using the <code>Console.WriteLine<\/code> every time you write a string to the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Console.WriteLine(<span class=\"hljs-string\">\"Hello, World!\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can use the <code>using static<\/code> syntax like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">using <span class=\"hljs-keyword\">static<\/span> System.Console;\n\nWriteLine(<span class=\"hljs-string\">\"Hello, World!\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By doing this, you can use the <code>WriteLine<\/code> method everywhere in the file without specifying the <code>Console<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Visual Studio to create a Console App project for creating a command-line program that runs on .NET core on Windows, macOS, and Linux.<\/li>\n\n\n\n<li>Use <code>Console.WriteLine<\/code> method to write a string to the console window.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"42\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-hello-world\/\"\n\t\t\t\tdata-post-title=\"C# Hello World\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"42\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-hello-world\/\"\n\t\t\t\tdata-post-title=\"C# Hello World\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to create a simple program that displays the famous message Hello, World! on a console window. Creating the C# hello world program First, launch Visual Studio. Next, click the Create a New Project button: Then, select the Console App to create a project for a command-line application that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-42","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/42\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/42\/revisions\/758"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}