C# Workshop – Output

Standard

Output

In this first example, you will learn about how to write some code to display Hello World, this is a classic programming example that’s traditional start to learning any programming language!
Visit dotnetfiddle.net and in the large box define which namespace to use entering the following:

using System;

System is used to allow access to the Console to output “Hello World”, a namespace is a way of organising groups of programming elements that can be used in the programme and the using statement should end with a semicolon like most statements in C#.

The next thing to do is to define the class for the example called Demo by entering in to dotnetfiddle.net the following:

public class Demo
{

}

Public is a Keyword that defines the access level of a class and how it may be used by other parts of the code and in this case, anything can access it. The class Keyword is used to define classes and this also uses curly braces to define the scope of the class.
The next thing to define is the Main Method which is the entry point for the example and is the first code to be run – this Method should be defined as below:

public static void Main()
{

}

Like a class, a method can also have an access level and again this is public so anything can access it, static defines this Method as global but will go into this concept in more detail later and the void Keyword means the Method doesn’t return a value and the brackets define any Parameters or none in this case, we’ll learn about those later too.

Within the Main Method in dotnetfiddle.net the following should be entered:

Console.WriteLine("Hello World");

Console is a type in the System namespace that can be used, here the WriteLine method is called and is like the Main Method in the example as it is also declared as being static. Inside the brackets is a value being passed as a Parameter, this is a string which is a set of text characters, in this case “Hello World”, the whole application in dotnetfiddle.net should look as below:

using System;

public class Demo
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}

You can then use the Run option to start the programme which will display the text “Hello World” in the lower large box on dotnetfiddle.net or if Auto Run is Yes then it will run automatically.
This programme structure will be used in all subsequent examples so they’ll always start with the using statements, followed by defining the class then the Main Method the example will use as its entry point which is the first point that will be reached when the example is started.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s