First C# Program Sample using Visual Studio

Now you have the basic knowledge of C# and history of C#, so we will take a look on how to create first program of C#, and  I will also explain the code, line by line.

So in this example, we will create program to print "Hello World" in C#.

What tools you need to create C# program?

You will need a Visual Studio, you can download the setup program file from official Microsoft website https://visualstudio.microsoft.com/downloads/ or you also download the ISO links of Visual Studio

If none of the above options works for you and you want to get started quickly, you can use online .NET Editor.

Let's begin creating "Hello World" program in Visual Studio 2017 Community Version ( Community version is available for free to use for individual developers and students).

If you have Visual Studio, you will be creating this application as a Console Application.

So, open your Visual Studio, select "File"->"New"->"Project"

new-project-visual-studio-create-min.png

Now, in the Next dialog box, Select "Windows Classic Dekstop" from left pane and "Console App (.NET Framework)" from right pane, provide a name to project "HelloWorldApp", change location to save project (if needed) and click "OK"

/new-project-visual-studio-console-min.png

In the Program.cs, which is opned by Default use the code below

using System;
				
namespace HelloWorldApp
{
    class HelloWorldApp
    {
         static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

Now, let's understand the above code:

  1. namespace: Namespace is a collection of collection of classes and sub namespaces. These sub namespaces are also namespaces in their own scope, but may have to be referenced using the parent namespace.
  2. class: it’s a keyword which is used to define class. A class is a piece of code that defines how a class of objects should behave, and what properties they can have.
  3. static: It is a keyword which is used to declare the class variable. We don’t need to create object to access static variable.
  4. void: void is a return type of the main method.
  5. Main: Method is a logical representation of a behavior. Methods can be used to create functionalities for a class. A method can have a scope, the type of data it returns, a name to be referenced from other code, list of arguments that it can take.
    Main is the method name, in console application , main method is the method which is required and runs/executes first.
  6. string[] args: It is string array which is used to store the command line arguments. Writing this is optional.
  7. Console.WriteLine(“Hello World”): WriteLine is the method used for writing on the console application. In this code line, we print the "Hello World" string to the console. To print a message to the console, we use the WriteLine() method of the Console class. The class represents the standard input, output, and error streams for console applications. Note that Console class is part of the System namespace. This line was the reason to import the namespace with the using System; statement. If we didn't use the statement, we would have to use the fully qualified name of the WriteLine() method: System.Console.WriteLine("Hello World");
  8. Using system: These are usually called as Imports, we are importing just one namespace here "System", We can import other namespaces and use them in our program, but here only "System" namespace is required, because of Console.WriteLine method.

In this example, we are saying ‘Hello World’ by printing it in the Console.

You can click on the "Start" button inside Visual Studio, to build and run this sample console application.

Output:

output-first-sample-csharp-application-min.png

Note: I am using Console.Readkey() to show you line also, to keep console app runnning and show you output, which is not required, but you can use it if you are using Visual Studio.

C# console reading values

As you can see in the above image we are reading User Key value using Console.ReadKey(), so basically using Console Class we can read values also.

using System;

namespace ReadLine
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?");

            string name = Console.ReadLine();
            Console.WriteLine("Hello {0}", name);
        }
    }
}

When exeucting above code, in the above code Console.ReadLine() reads a value from a console and prints it. 

We read a line from the terminal. When we hit the Enter key, the input is assigned to the name variable. The input is stored into the name variable, which is declared to be of type string.

Console.WriteLine("Hello {0}", name);

Output:

What is your Name? John //press "Enter" key after entering name
Hello John

Share Tweet