Introduction to C# and .NET
From the Cs curriculum
Introduction to C# and .NET
TL;DR
C# is a powerful programming language, and .NET is a framework that provides tools and libraries for building many types of applications using C#. Together, they let you create robust software for web, desktop, mobile, and cloud. Think of C# as the language you speak, and .NET as the ecosystem that makes your speech understandable and useful across different platforms.
1. The Mental Model
Imagine C# is a language like English or Spanish. .NET is the entire cultural context, including dictionaries, grammar rules, and all the common phrases people use, which allows you to build anything from a simple note to a complex novel using that language.
2. The Core Material
C# (pronounced "C sharp") is an object-oriented programming language developed by Microsoft. It's often used with the .NET framework, which is a big development platform for building various applications.
What is C#?
C# is a modern, general-purpose, and type-safe language. It's designed to be simple, powerful, and productive. You can use C# to write code for:
* Web applications (like websites and APIs)
* Desktop applications (Windows apps)
* Mobile apps (iOS, Android, using technologies like Xamarin/MAUI)
* Games (especially with Unity)
* Cloud services and much more.
A key feature of C# is its readability and strong typing, meaning you usually declare the type of data a variable will hold (e.g., int for whole numbers, string for text).
// This is a simple C# program
using System; // 'using' lets you access features from a 'namespace'
public class HelloWorld // A class is a blueprint for objects
{
public static void Main(string[] args) // This is the program's entry point
{
Console.WriteLine("Hello, C# World!"); // Prints text to the console
}
}
What is .NET?
.NET (originally .NET Framework, now commonly referred to as .NET or .NET Core) is a free, cross-platform, open-source developer platform for building many different types of applications. It includes:
- A Runtime: This is like a virtual machine that executes your C# code. For .NET, it's called the Common Language Runtime (CLR). It handles things like memory management and security.
- A Class Library: A vast collection of pre-written code (like functions and classes) that you can use in your applications. This saves you from writing common tasks from scratch, such as reading files, connecting to databases, or handling user input.
- A Set of Development Tools: Compilers, debuggers, and other utilities that help you write, test, and deploy your C# applications.
The beauty of .NET is its ability to run applications across different operating systems (Windows, macOS, Linux) thanks to its cross-platform nature and CLR.
Here's how C# and .NET work together:
graph LR
A["You write C# Code"] --> B["C# Compiler (part of .NET SDK)"];
B --> C["Intermediate Language (IL) Code"];
C --> D["Common Language Runtime (CLR)"];
D --> E["Runs on OS (Windows, macOS, Linux)"];
E --> F["Your Application Works!"];
Key Components
- CLR (Common Language Runtime): The execution engine for .NET applications. It compiles IL code into native machine code (Just-In-Time compilation or JIT), manages memory (garbage collection), and handles exceptions.
- Base Class Library (BCL): The fundamental library for .NET. It provides types for common functionalities like strings, dates, I/O operations, and collections.
- Frameworks built on .NET:
- ASP.NET Core: For building web applications and services.
- Windows Forms/WPF: For building traditional desktop applications on Windows.
- Xamarin/.NET MAUI: For building cross-platform mobile and desktop apps.
- Unity: A game development platform that uses C# and its own runtime.
3. Worked Example
Let's say you want to write a tiny C# program that asks for your name and then greets you. You'll use features from the .NET's Base Class Library (specifically, Console for input/output).
using System; // We need this to use Console features
public class Greeter // Define a class named Greeter
{
public static void Main(string[] args) // The starting point of our program
{
// 1. Prompt the user for their name
Console.Write("Please enter your name: "); // Console.Write prints without a new line
// 2. Read the name provided by the user
string userName = Console.ReadLine(); // Console.ReadLine reads a line of text
// 3. Construct a greeting message
string greetingMessage = $"Hello, {userName}! Welcome to C# and .NET.";
// 4. Display the greeting message
Console.WriteLine(greetingMessage); // Console.WriteLine prints with a new line
}
}
When you run this code:
1. The program will output Please enter your name:
2. It will then wait for you to type something and press Enter.
3. If you type Alice and press Enter, the program will then output Hello, Alice! Welcome to C# and .NET.
This simple example shows C# code using .NET's System.Console class to interact with the user via text.
4. Key Takeaways
- C# is an object-oriented programming language, primarily used with the .NET platform.
- .NET is a comprehensive developer platform that includes a runtime (CLR), libraries (BCL), and tools for building applications.
- Together, C# and .NET enable you to build a wide range of applications for web, desktop, mobile, and cloud.
- The CLR is responsible for executing your C# code and managing resources like memory.
- The BCL provides ready-to-use functionalities, making development faster and easier.
-
C# code is compiled into Intermediate Language (IL) first, which the CLR then converts to machine code.
-
Common mistakes to avoid:
- Confusing C# (the language) with .NET (the platform); they work together but aren't the same.
- Not understanding that
using System;allows you to access fundamental classes likeConsole. - Forgetting that
Mainis the entry point for most C# console applications. - Expecting C# to run without the .NET runtime environment.
5. Now Try It
Install the .NET SDK (Software Development Kit) from the official Microsoft website. Then, create a new C# console project using the dotnet new console command. Modify the default "Hello World" program to ask for your favorite color and then print a message like "Wow, [YourColor] is a great choice!"
Success looks like: You have the .NET SDK installed, can create a new project, modify its Program.cs file, and run it from your terminal to see the personalized color message.
Frequently asked about Introduction to C# and .NET
More from Cs
Get the full Cs curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account