You are on page 1of 6

C# Tutorial

Hello World
To create a simple Hello World program in Visual Studio, open
File, select New, and select Project. Select Visual C# from the
left pane and select Console Application. The IDE will display
lines of codes. Within the codes, locate the inner most part of
the syntax which is here:
static void Main(string[] args)
{

Inside these curly braces, type the following codes:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

Installing the .Net SDK


This is an essential part of our tutorial, to learn how to code
in C# using the latest .Net SDK of Microsoft for C#. This
framework helps us to build Android or Mac apps. The first thing
we have to do is to go to this website:
https://dotnet.microsoft.com/learn/dotnet/hello-world-
tutorial/install
And follow the SDK download links provided depending on the type
of processor that you have.
Check everything installed correctly
Once you've installed, open a new command prompt and run the
following command (not as Administrator):
dotnet

If the installation succeeded, you should see an output similar


to the following:

Usage: dotnet [options]


Usage: dotnet [path-to-application]

Options:
-h|--help Display help.
--info Display .NET information.
--list-sdks Display the installed SDKs.
--list-runtimes Display the installed runtimes.

path-to-application:
The path to an application .dll file to execute.

Got an error?
If you receive a 'dotnet' is not recognized as an internal or
external command error, make sure you opened a new command
prompt. If you can't resolve the issue, use the I ran into an
issue button to get help fixing the problem.

Create your app


In your command prompt, run the following command to create your
app:

dotnet new console -o myApp

Then, navigate to the new directory created by the previous


command:

cd myAppWhat do these commands mean?

 The dotnet new console command creates a new console app for


you.
 The -o parameter creates a directory named myApp where your
app is stored and populates it with the required files.
 The command cd myApp changes your current directory to the
one just created for the new app.
The main file in the myApp folder is Program.cs. By default, it
already contains the necessary code to write "Hello World!" to
the Console.

Program.cs
using System;

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

Got an error?
If you receive a message similar to Template "Console
Application" could not be created. Access to the path
'C:\Windows\System32\myApp' is denied., change your current
directory to one where you have permissions to create a new
folder and try to run the command again. If you can't resolve the
issue, use the I ran into an issue button to get help fixing the
problem.

Run your app


In your command prompt, run the following command:

dotnet run

Congratulations, you've built and run your first .NET app!

Edit your code

Open the Program.cs file in any text or code editor, such as


Notepad or Visual Studio Code. The Program.cs file is located on
the newly created myApp directory.

Then, add the highlighted line after the code that prints "Hello
World!", like the following:

Program.csCopy
using System;

namespace myApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("The current time is " + DateTime.Now);
}
}
}

Save the Program.cs file and run your code again:

dotnet run

If you succeed, you should see an output similar to the


following:

Hello World!
The current time is 11/10/2020 8:00:00 AM

Parts of a program

using System; = System is also a namespace. Inside this “using”


we have different classes, one of which is the “console” class in
the statement. The complete name of “console” class is
System.Console.WrtiteLine (), but instead including “System” in
the statement, we can actually omit this because its already in
the “using System”. If we will include “System” in our statement,
this statement will be called as fully qualified statement.
namespace Yee_Tutorial = This is the namespaces - organization
that groups the classes
class Program = this is the class, and the name is Program. It
has members which are called methods.
static void Main(string[] args) = this is the method - it
contains a block of codes. This method was called Main. It is a
rule that the name of this method must be “Main” (should not
starts with small cap).
A method can be determined by its open and close parenthesis ().
So, in the statement shown below, we can definitely say that
“WriteLine()” is a method and “Console” is the class.
Console.WriteLine("Hello World! This is the beginning of our C#
Tutorials"); = this is a statement. Inside statement we have the
Console as the class, the WriteLine as the method and inside the
parenthesis is the “argument”.
So to summarize, the entire structure of a program is:
Namespace > classes > members (such as methods) > statements

The Void and static methods


Static methods are methods that return results. Once we called a
certain method static, it can call any methods directly like the
WriteLine method and execute the command. However, in some cases,
methods without static can call the sub-methods directly by they
cannot execute the command, these are called void methods,
because it doesn’t return any result. In this case, the void
method must have an instance.
So in this program below, we will see and void method, that never
return any result after started.
Therefore, in able for us to obtain result from a void method, we
have to create an instance, where an object of an instance called
“Yee1” has been created. Once you run this program, the void
method will give you a result.

Arguments
Arguments are the data being passed into the method (normally
seen inside the open and close parenthesis, like this one:
Console.WriteLine("Hello World");

24:38
Path: C:\Users\Yee\source\repos

You might also like