.NET Introduction
What is .NET?
.NET (pronounced "dot net") is a free, cross-platform, open-source developer platform created by Microsoft for building many different types of applications. With .NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, games, IoT, and more.
The .NET platform includes:
- Programming languages: C#, F#, and Visual Basic
- Runtime environments: .NET Core, .NET Framework, Mono
- Libraries: Base Class Library (BCL), ASP.NET, Entity Framework, etc.
- Development tools: Visual Studio, Visual Studio Code, command-line tools
A Brief History of .NET
- 2002: Microsoft released the first version of .NET Framework
- 2016: .NET Core was introduced as a cross-platform, open-source alternative
- 2020: .NET 5 unified the ecosystem, merging .NET Core and .NET Framework
- Present: .NET continues to evolve with regular updates and improvements
Why Use .NET?
- Cross-platform: Runs on Windows, macOS, and Linux
- High performance: Optimized for speed, memory usage, and scalability
- Open source: Community-driven development with transparent roadmap
- Rich ecosystem: Extensive libraries and tools for various application types
- Enterprise support: Backed by Microsoft with long-term support options
Getting Started with .NET
1. Installing .NET SDK
The .NET SDK (Software Development Kit) includes everything you need to build and run .NET applications. To install it:
- Visit dotnet.microsoft.com/download
- Download the .NET SDK for your operating system
- Follow the installation instructions
After installation, verify it worked by opening a terminal or command prompt and typing:
dotnet --version
This should display the installed .NET version.
2. Your First .NET Application
Let's create a simple "Hello World" console application:
- Open a terminal or command prompt
- Create a new console application with:
dotnet new console -n MyFirstApp
- Navigate to the application directory:
cd MyFirstApp
- Open the
Program.cs
file, which should look something like this:
namespace MyFirstApp;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
- Run the application:
dotnet run
Output:
Hello, World!
Congratulations! You've just created and run your first .NET application.
Key Components of the .NET Platform
Common Language Runtime (CLR)
The CLR is the execution engine that handles running .NET applications. It provides:
- Memory management through automatic garbage collection
- Type safety
- Exception handling
- Thread management
- Just-In-Time (JIT) compilation
Base Class Library (BCL)
The BCL is a comprehensive collection of reusable types (classes, interfaces, etc.) that you can use to develop applications. It includes types for:
- File I/O operations
- String manipulation
- Data collection management
- Networking
- Database access
- And much more
Application Models
.NET supports various application models:
- Web applications with ASP.NET Core
- Desktop applications with WPF, Windows Forms, or MAUI
- Mobile apps with .NET MAUI
- Cloud services with Azure Functions and ASP.NET Core
- IoT applications with .NET IoT
- Game development with Unity and .NET
C# - The Primary .NET Language
C# (pronounced "C sharp") is the most widely used language for .NET development. Let's look at some basic C# syntax:
Variables and Data Types
// Variable declaration and initialization
string name = "John";
int age = 30;
bool isEmployed = true;
double salary = 50000.50;
// Constants
const double Pi = 3.14159;
// Implicit typing
var message = "Hello"; // Compiler infers this is a string
Control Structures
// If-else statement
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
// For loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Iteration {i}");
}
// While loop
int counter = 0;
while (counter < 3)
{
Console.WriteLine($"Count: {counter}");
counter++;
}
Object-Oriented Programming
// Class definition
public class Person
{
// Properties
public string Name { get; set; }
public int Age { get; set; }
// Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
// Method
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
}
// Using the class
Person person = new Person("Alice", 25);
person.Introduce();
Output:
Hi, I'm Alice and I'm 25 years old.
Practical Example: Building a Simple Calculator
Let's create a more complete application - a simple calculator that performs basic operations.
using System;
namespace SimpleCalculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Simple Calculator");
Console.WriteLine("----------------");
// Get first number
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
// Get second number
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
// Get operation
Console.Write("Enter operation (+, -, *, /): ");
string operation = Console.ReadLine();
// Perform calculation
double result = 0;
switch (operation)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0)
{
result = num1 / num2;
}
else
{
Console.WriteLine("Error: Cannot divide by zero!");
return;
}
break;
default:
Console.WriteLine("Invalid operation!");
return;
}
// Display result
Console.WriteLine($"Result: {num1} {operation} {num2} = {result}");
}
}
}
Sample run:
Simple Calculator
----------------
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): *
Result: 10 * 5 = 50
Common .NET Tools and Libraries
NuGet Package Manager
NuGet is the package manager for .NET, allowing you to add third-party libraries to your projects.
Example of installing a package via command line:
dotnet add package Newtonsoft.Json
Using the package in your code:
using Newtonsoft.Json;
string jsonString = "{\"Name\":\"John\", \"Age\":30}";
Person person = JsonConvert.DeserializeObject<Person>(jsonString);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
LINQ (Language Integrated Query)
LINQ allows you to write query expressions directly in C# to work with collections of data:
// Example of using LINQ to filter a list
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
Console.WriteLine("Even numbers:");
foreach (var num in evenNumbers)
{
Console.Write($"{num} ");
}
// Output: Even numbers: 2 4 6 8 10
Summary
In this introduction to .NET, you've learned:
- What .NET is and its key components
- How to install the .NET SDK and create your first application
- Basic C# syntax and programming concepts
- How to create a practical application (a simple calculator)
- About common tools and libraries in the .NET ecosystem
.NET is a powerful, versatile platform that enables you to build a wide range of applications. As you progress in your learning journey, you'll discover more advanced features and capabilities.
Additional Resources
- Official .NET Documentation
- C# Programming Guide
- .NET GitHub Repository
- Microsoft Learn .NET Courses
Exercises
- Modify the "Hello World" application to ask for the user's name and then greet them personally.
- Extend the calculator application to include additional operations like exponentiation and modulo.
- Create a simple console application that maintains a to-do list where users can add, view, and delete tasks.
- Build a basic console-based game like "Guess the Number" using .NET concepts you've learned.
Happy coding with .NET!
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)