Learn C#

Learn C# Together ~ Reading and Writing to a console


This is the official first lesson after we did the introduction last week. We are going to get you started on writing a simple ‘Hello World’ type console application, just like in any other language beginning. We will learn the basic structure of a C# program. We will assume that you have downloaded a version of Visual Studio and are at least capable of opening a new project.

Below it’s the code for the first program that we talked about and this is a good intro to the language.

// Namespace declaration
using System;

class TempleOfTech
    public static void Main()
        // Write to console
        Console.WriteLine (“Welcome to Learn C# Together – Series “); 

The using keyword is used for including the namespaces in the program. A program can include multiple using statements.

Understand the purpose of using System declaration – The namespace declaration, using System, indicates that you are using the System namespace. If  you omit the using System, declaration, then you have to use the fully qualified name of the Console class. A namespace is used to organize your code and is collection of classes, interfaces, structs, enums and delegates. We will discuss about namespaces in detail in a later post.

The purpose of Main() method is to indicate the starting point into the application. And remember there is only one Main method per Class.

Console.WriteLine() is the function that like it says writes to the console for us to view. The text we want to display should be inside “Quotes” that is how it’s syntax works. Now to understand why we have included the namespace System look how the function would have looked without it.
System.Console.WriteLine() – You see this can get annoying really fast so the solution is to import the library and have shorter more precise names for the functions.

Now let’s write a program that takes our name as an input and returns it with a welcoming message.

using System;
class Program
    static void Main()
        // Prompt the user for his name
        Console.WriteLine(“Please enter your name : “);
        // Read the name from console
        string UserName = Console.ReadLine();

        // Concatenate name with hello word and print name
        Console.WriteLine(“Hello ” + UserName);

        //Placeholder syntax to print name with hello word 
        //Console.WriteLine(“Hello {0}”, UserName);

Please take note that C# is a case sensitive programming language!
So to begin we write to the console asking for an input by the user. And to get that input we use Console.ReadLine(); function. In the code we have assigned it to a string immediately.
Later we showcase the different ways to write to console , concatenation and place holder syntax. The latter is most preferred.

You Might Also Like

1 Comment

Leave a Reply

%d bloggers like this: