Your First C# Application
Welcome to the world of C#! This guide will walk you through creating your very first C# application. We'll start with a simple "Hello, World!" program, which is a traditional first step in learning any new programming language.
Prerequisites
Before you begin, ensure you have the following installed:
- .NET SDK: You can download it from the official .NET website.
- A Code Editor or IDE: Popular choices include Visual Studio Code (free and cross-platform), Visual Studio Community (free for individuals and open-source projects), or JetBrains Rider.
Creating Your Project
We'll use the .NET CLI (Command Line Interface) to create our project. Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:
dotnet new console -o MyFirstApp
This command does the following:
dotnet new console: Creates a new console application project.-o MyFirstApp: Specifies the output directory for the project. This will create a new folder namedMyFirstApp.
Now, change your directory into the newly created project folder:
cd MyFirstApp
Exploring the Project Files
Inside the MyFirstApp folder, you'll find a few files. The most important one for now is Program.cs. This is where your application's code will reside.
Writing Your First Code
Open the Program.cs file in your code editor. You'll likely see something like this:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Let's break down this code:
using System;: This line imports theSystemnamespace, which contains fundamental classes and base types, including theConsoleclass we'll use for output.namespace MyFirstApp: Namespaces help organize code and prevent naming conflicts.class Program: In C#, all executable code resides within classes.Programis the name of our main class.static void Main(string[] args): This is the entry point of your application. When you run the program, the code inside theMainmethod is executed first.static: Means the method belongs to theProgramclass itself, not to any specific instance of the class.void: Indicates that the method does not return any value.Main: The conventional name for the entry point method.string[] args: Represents command-line arguments that can be passed to the application.
Console.WriteLine("Hello, World!");: This is the line that does the work!Console: A class from theSystemnamespace that provides input/output functionality.WriteLine: A method of theConsoleclass that prints a line of text to the console and then moves to the next line."Hello, World!": The string literal that will be displayed.
Running Your Application
To run your application, go back to your terminal or command prompt, make sure you are still in the MyFirstApp directory, and run the following command:
dotnet run
You should see the following output in your terminal:
Hello, World!
Next Steps
Congratulations! You've successfully created and run your first C# application. This is just the beginning. From here, you can explore variables, data types, control flow statements, and much more to build more complex and powerful applications.
Continue your journey by exploring the other sections of our C# documentation:
Explore Variables and Data Types