Welcome to the world of C#!
C#, pronounced “C-Sharp,” is a versatile and powerful programming language developed by Microsoft. It’s widely used for building applications, ranging from desktop software to web and mobile apps.
In this post, we will guide you through writing your first C# program. Whether you’re a beginner or just looking to refresh your knowledge, this guide will help you take the first step.
Setting Up Your Environment
Before we write any code, ensure you have the following:
- A Code Editor: Install Visual Studio, which is a robust Integrated Development Environment (IDE) for C# development.
- .NET Framework or .NET Core: These are required to run C# programs. Visual Studio usually installs them for you.
Writing Your First C# Script
- Open Visual Studio and create a new project.
- Select the Console App (.NET Core) template and name your project “HelloWorld”.
- Replace the default code in
Program.cs
with the following:
using System;
class Program
{
static void Main(string[] args)
{
// This is your first C# program
Console.WriteLine("Hello, World!");
}
}
Explanation of the Code
using System;
: This imports the System namespace, which contains fundamental classes and base functionality.class Program
: Defines a class namedProgram
.static void Main(string[] args)
: TheMain
method is the entry point of a C# program.Console.WriteLine("Hello, World!");
: This prints “Hello, World!” to the console.
Running Your C# Program
- Press
Ctrl + F5
to build and run your program. - You should see the output:
Hello, World!
Congratulations! You’ve successfully written and executed your first C# program.
What’s Next?
Now that you’ve written your first program, explore more C# concepts like variables, loops, and classes. C# opens the door to endless possibilities in software development.
Happy coding!