A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 3.1: Basic Syntax and Data Types


a. Hello World Program

Let's begin by creating a simple "Hello World" program in C#. This classic example helps us understand the basic syntax and structure of a C# program.

1. Create a New Console Application:

If you haven't done so already, open Visual Studio and create a new console application following the steps mentioned in Tutorial 2.

2. Write the "Hello World" Code:

Open the Program.cs file in the code editor and replace the existing code with the following:


using System;
class Program
{
    static void Main()
    {
        // This is a single-line comment

        /*
        This is a
        multi-line comment
        */

        // Displaying "Hello, World!" on the console
        Console.WriteLine("Hello, World!");
    }
}

3. Understand the Code:

  • using System;: This line is an example of a using directive. It allows you to use types from the System namespace without specifying the full namespace every time.

  • class Program: This declares a class named Program. In C#, the Main method, where the program starts executing, is typically located in a class named Program.

  • static void Main(): This is the entry point of the program. Main is a method (function) that serves as the starting point for program execution.

  • Console.WriteLine("Hello, World!");: This line uses the Console class to write the string "Hello, World!" to the console. WriteLine adds a newline after the specified text.

4. Run the Program:

Press F5 or click the "Start Debugging" button to build and run your program. You should see the output in the console window: "Hello, World!"

5. Comments in C#:

  • Single-line comments: Created using //. Anything after // on a line is a comment.

  • Multi-line comments: Enclosed between /* and */. Everything between these markers is a comment.

6. Experiment:

  • Try modifying the "Hello, World!" text.
  • Add your comments to the code.
  • Experiment with different Console class methods, like Console.Write or formatting options.

Congratulations! You've successfully created and run a basic C# program. In the next tutorials, we'll explore variables, data types, and control flow statements.