C# Fundamentals

We will see our first C# program and learn about the entry point of every C# program. From there, we will see and learn how to display something to user and take user input the C# way.

Hello World Program👋

See here 👉 How to create your first Hello World Program in C# using Visual Studio

See here 👉 How to create your first Hello World Program in C# using .NET CLI

Here is a classic “Hello World” console program written using C#: (code and output)

using System;

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

fundamentals

Let’s dissect each piece and explain


using System;

First line of our program is the using System which basically appears in all C# programs.

It is our way in C# to use and access the codes and classes defined in “System” namespace. In the case of our ‘Hello, World’ program, the Console.WriteLine(“Hello, World”); will not work if we did not use the using directive in System namespace because the Console.WriteLine() is in the System namespace.


namespace HelloWorld

On the second line, we have “namespace”.

Namespace in C# is simply a logical collection of related classes.

The main purpose of using namespace is to prevent name conflicts. In our code above, we are saying that the class Program is inside or within the HelloWorld namespace.


class Program

The third line contains a class declaration, all of C# program contains at least one class. In our code, the name of our class is Program.

Class in C# basically is the structure that contains the data (variables, properties) and functions (methods). We can see up in our code that our class Program contains one method with the name of Main().


static void Main(string[] args)

We can see the Main() method on the next line, the entire line of code is the standard signature of Main() method in C#.

This method is very important as it is the entry point of our program which means our program starts its execution from the first line of the Main method.

In C#, it is legitimate to have multiple Main() methods, but you need to explicitly identify which Main method is the entry point at the runtime.


Console.WriteLine("Hello, World!");

The next line is the code that prints “Hello, World!” on the Console screen.

Here we called the WriteLine, a static method in Console class defined in the System namespace. This method takes the string (enclosed in double quotes) as its parameters and prints it on the Console window.

In C#, like other object-oriented languages, uses the dot (.) operator to access the member variables (fields) and methods of a class.

Also, braces () are used to identify methods in the code and string literals are enclosed in double quotation marks ().

Lastly, each statement in C# (like C, C++ and Java) ends with a semicolon (;), also called the statement terminator.

Important Points to Remember

  • Your C# executable program resides in some class
  • The entry point to program is the static method Main() with void return type
  • C# is case sensitive language so void and Void are different
  • Whitespaces (enter, tab, space) are ignored by the compiler between the code.
  • There can be multiple Main method
  • The boundaries of namespace, class and methods are defined by opening and closing curly brackets { }
  • A namespace is only a logical collection of classes with no physical mapping on disk (unlike Java)
  • The using keyword is used to inform compiler where to search for the definition of class (namespaces) that you are about to use in your C# program
  • It is not mandatory that Main method of program takes string[] args as parameter.

Interactive Hello World Program 👏

Let’s now see how to accept input from a user in C#.

Consider our second program below, it will ask the user for their name and will greet using his/her name.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Hello " + name + ", welcome to C#.");
        }
    }
}

fundamentals

Let’s see the code inside the Main method


Console.Write("Please enter your name: ");

In the first line, we have used another method, Write() method of the Console class.

This is similar to the WriteLine() method in our first program, the difference is that using Write() does not change the cursor to the next line which means the next output is right next to it instead of on the next line.


string name = Console.ReadLine();

In the second line, we declared a variable of type string and the name of our variable is name.

Then we take an input from the user using the ReadLine() method of the class Console and put that input as value of the variable name.


Console.WriteLine("Hello " + name + ", welcome to C#.");

In the third line, we printed the name given by user in line 2, along with some greeting text using WriteLine() method of the Console class.

There, we used a concatenation operator (+).


🔚 end of document 🔚

References