Part 1: Introduction to C# and .NET
What is C# and what is .NET?
When you first hear about C# and .NET, it might seem a bit confusing as they’re often mentioned together. In reality, they’re two distinct things that complement each other perfectly to help you create all sorts of applications.
- C# is a programming language created by Microsoft back in the early 2000s. They designed it to be simple to learn but powerful. Originally, it was mainly used for Windows applications, but today it’s much more flexible and can be used on various platforms, like Linux, Mac, Android, and iOS.
- .NET, on the other hand, is what’s called a framework. .NET provides lots of tools, libraries, and features that make development easier, helping your code work smoothly across different environments.
So, you can think of C# as the "language" we use to communicate with the computer, and .NET as a "toolbox" that helps you build just about anything you can imagine.
A Bit of History
Microsoft launched C# in 2002, alongside the first version of the .NET Framework. Their goal was to compete with Java (which was already very popular at the time) and to offer developers an alternative that was just as powerful and easy to learn.
Over time, both C# and .NET have evolved significantly. One of .NET's major milestones was the release of .NET Core in 2016, which made it possible to create cross-platform applications (Linux, macOS, and more) and optimised the code to run faster and more efficiently.
Why Use C# and .NET?
Here are some reasons why learning C# and .NET is a great investment:
- Productivity: C# has a clear syntax, making it straightforward to learn. .NET also comes with thousands of libraries to simplify common tasks (like file handling or network management), saving you loads of time.
- Cross-Platform: With the latest versions of .NET, you can create applications for Windows, Linux, and macOS without switching languages.
- Performance: C# and .NET are optimised to create fast, efficient applications.
- Community and Resources: The C# and .NET community is large and well-established, meaning you’ll have access to tons of tutorials, examples, and support when you need it.
What Can You Create with .NET?
With C# and .NET, you can develop practically any type of application:
- Desktop Applications: You can create graphic applications for Windows, ranging from simple tools to complex business applications.
- Web Applications: Using ASP.NET (the web framework for .NET), you can develop websites and backend services.
- Mobile Apps: With Xamarin (a tool within .NET), you can create apps for Android and iOS using the same codebase.
- Games: If you're interested in game development, the Unity engine uses C# as its scripting language.
- Cloud Services: C# and .NET are widely used in the cloud, especially with Azure (Microsoft’s cloud platform).
Setting Up Your Environment: Configure the .NET CLI
Before starting, make sure you have the .NET CLI installed. This is a command-line tool that lets you create, compile, and run .NET projects directly from the terminal, which is great for managing projects without relying solely on a graphical interface. Plus, you can automate many tasks this way.
"Hello Codú"
To get started with C#, we’ll make the classic "Hello, Codú!" programme. This small programme helps you see how the code is structured and ensures everything is working correctly.
Step 1: Set Up the Environment
To run this example, you’ll need to install:
- Visual Studio or Visual Studio Code (to write and manage the code).
- .NET SDK (version 5 or later).
Step 2: Create the Project
Open a terminal or command prompt.
Type the following command to create a new console project:
dotnet new console -o HelloCodu
This command creates a folder called
HelloCodu
containing the files needed for your project.Navigate to the project folder:
cd HelloCodu
Step 3: Understanding the Project Structure
Inside the HelloCodu
folder, you’ll see a few files and folders:
Program.cs
: This is where you’ll write your code.obj
andbin
: Automatically generated folders containing build files.
Here’s a quick look at the project structure:
HelloCodu/ │ ├── Program.cs <-- This is where you write your code ├── obj/ <-- Temporary build files └── bin/ <-- Compiled files ready to run
Step 4: Write the Code
Open Program.cs
in your editor and enter this code:
using System; namespace HelloCodu { class Program { static void Main(string[] args) { Console.WriteLine("Hello, Codú!"); } } }
Step 5: Run the Programme
In the terminal, make sure you’re in the project folder and type:
dotnet run
You should see the message:
Hello, Codú!
What Does Each Part of the Code Do?
- using System;: This tells C# we’ll be using the
System
namespace, which includes helpful classes likeConsole
. - namespace HelloCodu: Defines a namespace to organise the code.
- class Program: Declares a class named
Program
, which contains our code. - static void Main(string[] args): This is the entry point of the programme. It’s the first line that runs when the programme starts.
- Console.WriteLine("Hello, Codú!");: Prints "Hello, Codú!" to the console.
Tips for Getting Started
- Comment Your Code: Writing comments helps you understand each step as you go along.
- Use Descriptive Names: Give variables and functions clear names so your code is easy to read.
- Follow a Good Format: Stick to styling conventions like using PascalCase for class names and camelCase for variables. This keeps your code looking professional and easy to understand.
Quick Summary
Here’s a quick recap of the steps:
- Create the project:
dotnet new console -o HelloCodu
- Write the code in
Program.cs
- Run the project:
dotnet run