C# Basic Syntax

Basic Syntax In C#

Using System: When you see using System;, think of it as opening a toolbox full of useful tools (classes and functions) for your C# project. The System namespace provides essential building blocks, like the Console class (for input/output) and the WriteLine method (for printing text). By including this line, you can directly access these tools without typing the full namespace every time.

Namespace: Imagine a namespace as a labeled box where you organize related items. In C#, namespaces group classes, methods, and other elements together. For example, the System namespace contains tools related to system operations.

Class: A class is like a blueprint or recipe for creating objects. Think of it as a template with data (variables) and actions (methods). In your case, the Program class is where your code starts executing.

Static: When something is static, it doesn’t change or move around. In C#, static means you don’t need an object to use certain tools (like Main). It’s like having a shared tool that everyone can use without creating a new one.

void: When a method returns void, it means it doesn’t give back any specific value. In your Main method, you’re not expecting a result; you’re just doing something (starting your program).

Main: The Main method is where your program begins. When you run your C# program, it’s like pressing the “start” button—it calls the Main method first.

string[] args: These are like extra instructions you can give your program when you run it. They’re called “command line arguments.” For example, you could pass a filename or a number to your program using these arguments.

Console.WriteLine(“Hello World!”): Imagine the Console as a screen where your program talks to you. WriteLine is like typing a message on that screen. So, your program says, “Hello World!” and shows it on the console.

For example,

using System;
namespace Application
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(“Hello World!”);
        }
    }
}

Explanation:

using System;

This line is an important statement. It allows your program to use classes and functions from the System namespace. The System namespace contains essential tools for input/output, file handling, and other system-related operations.

namespace Application

A namespace is like a container that groups related code together. Here, you’ve defined a namespace named Application. Inside this namespace, you can organize your classes and other elements.

public class Program

You’re defining a class named Program within the Application namespace. A class is a blueprint for creating objects. The public keyword means other parts of your program can access this class.

public static void Main(string[] args)

This is the entry point of your program. It’s where execution begins. The Main method is static, meaning you don’t need an object to call it. It accepts an array of strings (string[] args) as command line arguments (if any). Inside the Main method, you can write your program logic.

Console.WriteLine(“Hello World!”);

This line uses the Console class (from the System namespace). The WriteLine method prints the text “Hello World!” to the console. So, when you run this program, it will display “Hello World!” on the console. 🚀

Course Video