C# Coding Structure: Learn Basic Program Syntax

So, you want to learn C#?

Great choice! C# is a powerful and beginner-friendly programming language. But before you build cool apps, you need to understand the basics.

Let’s start with a simple C# program. Don’t worry—I’ll explain everything step by step in a super simple way. By the end, you’ll know how a basic C# program works and even run your own code.

Excited? Let’s go!

1. Understanding Structure of Basic C# Code

Let’s start with a simple C# program. Don’t worry if you’re new—I’ll break everything down so it all makes sense!

Basic C# Program

				
					using System;  

class Program  
{  
    static void Main()  
    {  
        Console.WriteLine("Hello, C#!");  
    }  
}
				
			

Now, let’s go step by step and understand what’s happening here.

Breaking Down the Code

1️⃣using System;

This line tells C# that we want to use the System namespace. Think of a namespace as a collection of useful tools. In this case, System gives us access to basic functions like printing to the console.

2️⃣class Program

A class is like a blueprint. Everything in C# lives inside a class. Here, we’ve created a class named Program. You can name it anything, but “Program” is a common choice for simple examples.

3️⃣static void Main()

This is the Main method, and it’s the entry point of every C# program. When you run your code, this method executes first.

  • static means this method belongs to the class itself, not an object of the class.
  • void means it doesn’t return anything.
  • Main() is the name of the method, and every C# program must have it.

4️⃣Console.WriteLine("Hello, C#!");

This line prints text to the console.

  • Console is a built-in class that helps us interact with the console window.
  • WriteLine() prints a message and moves to the next line.
  • "Hello, C#!" is the message we want to print. You can change it to anything you like!

How It Runs

  1. The C# compiler reads the code.
  2. It starts execution from Main().
  3. It finds Console.WriteLine() and prints “Hello, C#!”.
  4. The program ends.

Try It Yourself!

Now it’s your turn! Open Visual Studio, Visual Studio Code, or an online C# compiler, type the code, and run it. You’ll see "Hello, C#!" printed on the screen.

2. Taking User Input in C#: A Simple Guide

Now that you know how a basic C# program works, let’s make it more interactive! We’ll write a simple C# program that takes user input, performs an operation, and displays the result.

Don’t worry—I’ll explain everything step by step! Let’s get started.

Basic C# Program with User Input

				
					using System;  

class Program  
{  
    static void Main()  
    {  
        // Asking for user input
        Console.Write("Enter your name: ");  
        string name = Console.ReadLine();  

        Console.Write("Enter your age: ");  
        int age = Convert.ToInt32(Console.ReadLine());  

        // Performing a simple operation
        int nextYearAge = age + 1;  

        // Displaying output
        Console.WriteLine($"Hello, {name}! Next year, you will be {nextYearAge} years old.");  
    }  
}
				
			

Breaking Down the Code

1️⃣using System;

This allows us to use built-in C# tools like Console.WriteLine() and Console.ReadLine().

2️⃣class Program

Everything in C# is inside a class. Here, we named it Program.

3️⃣static void Main()

The Main() method is where the program starts running. Every C# program must have this.

4️⃣ Taking User Input

  • Console.Write("Enter your name: "); → Prints a message asking for input.
  • string name = Console.ReadLine(); → Reads the user’s input and stores it in a variable called name.
  • Console.Write("Enter your age: "); → Asks for age input.
  • int age = Convert.ToInt32(Console.ReadLine()); → Reads the input, converts it to an integer, and stores it in age.

💡 Why Convert?

Everything from Console.ReadLine() is read as a string. Since we need a number for age, we convert it using Convert.ToInt32().

5️⃣ Performing a Simple Operation

  • int nextYearAge = age + 1; → Adds 1 to the user’s age.

6️⃣ Displaying the Output

  • Console.WriteLine($"Hello, {name}! Next year, you will be {nextYearAge} years old.");
    • The $"" is used for string interpolation, making it easy to insert variables inside a string.

How It Works

  1. The program asks for your name and age.
  2. It reads and stores the input.
  3. It adds 1 to your age.
  4. It prints a message showing your name and how old you’ll be next year.
Output
				
					Enter your name: Steven
Enter your age: 25  
Hello, Steven! Next year, you will be 26 years old.  
				
			

How It Works Step by Step:

  1. The program asks for your name → You type “Steven” and press Enter.
  2. It asks for your age → You type “25” and press Enter.
  3. The program adds 1 to your age → 25 + 1 = 26.
  4. It prints “Hello, Steven! Next year, you will be 26 years old.”

Try running it yourself with different names and ages!

Try It Yourself!

Copy and paste the code into Visual Studio, Visual Studio Code, or an online C# compiler. Run it, enter your name and age, and see the magic happen!

Final Thought !

And that’s it! You just wrote and understood your first C# program. 🎉 Now, you know how C# programs are structured, how they run, and what each part does.

This is just the beginning! As you keep learning, you’ll discover more exciting features like taking user input, working with data, and even building applications. So, keep practicing, experiment with code, and have fun! Happy coding!

Leave a Comment

Share this Doc

Understand C# Code

Or copy link