C# Introduction and Setup
25 minC# is a modern, object-oriented programming language developed by Microsoft. Created by Anders Hejlsberg in 2000, C# is part of the .NET ecosystem and has evolved significantly with each version. C# combines the power and performance of C++ with the simplicity and safety of Java. Modern C# (C# 8.0+) includes features like nullable reference types, pattern matching, and async/await, making it a powerful, expressive language.
It's part of the .NET ecosystem and is widely used for Windows applications, web development, and mobile apps. .NET provides a comprehensive runtime, libraries, and tools for building various application types. C# can build desktop apps (WPF, WinForms), web apps (ASP.NET Core), mobile apps (Xamarin), games (Unity), and cloud services. Understanding C#'s versatility helps you choose it for diverse projects.
C# combines the power of C++ with the simplicity of Java, offering strong typing and garbage collection. C#'s type system prevents many common errors through compile-time checking. Automatic memory management (garbage collection) eliminates manual memory management bugs. C# supports both value types (stored on stack) and reference types (stored on heap), giving you control when needed. Understanding C#'s design helps you write safe, efficient code.
C# is a compiled language that runs on the .NET runtime (CLR - Common Language Runtime). The compilation process converts C# code to Intermediate Language (IL), which the CLR executes. This architecture enables language interoperability—code written in different .NET languages can work together. Understanding the .NET architecture helps you leverage the platform's capabilities.
Modern C# development uses .NET Core/.NET 5+ (now just .NET), which is cross-platform (Windows, Linux, macOS). This cross-platform capability, combined with C#'s performance and modern features, makes C# competitive with other modern languages. C# development typically uses Visual Studio or Visual Studio Code with C# extensions. Understanding the development environment helps you be productive with C#.
C#'s standard library (.NET Base Class Library) provides extensive functionality: collections, file I/O, networking, threading, LINQ, and more. LINQ (Language Integrated Query) enables querying data from various sources using SQL-like syntax. Understanding the standard library and LINQ helps you write concise, powerful C# code. C#'s async/await pattern makes asynchronous programming straightforward.
Key Concepts
- C# is a modern, object-oriented language in the .NET ecosystem.
- C# is strongly typed with automatic garbage collection.
- C# runs on .NET runtime (cross-platform with .NET Core+).
- C# supports both value types and reference types.
- C# includes powerful features like LINQ and async/await.
Learning Objectives
Master
- Setting up C# development environment (.NET SDK, IDE)
- Writing and compiling basic C# programs
- Understanding C# syntax and structure
- Using C# standard library and namespaces
Develop
- Understanding .NET ecosystem and architecture
- Appreciating C#'s role in modern application development
- Setting up efficient C# development workflows
Tips
- Install .NET SDK from microsoft.com/net/download.
- Use Visual Studio Code with C# extension or Visual Studio IDE.
- Create projects with: dotnet new console -n MyProject.
- Run with: dotnet run or dotnet build then execute.
Common Pitfalls
- Not installing .NET SDK, only installing runtime.
- Using wrong .NET version for your project.
- Not understanding namespaces, causing compilation errors.
- Confusing value types with reference types, causing unexpected behavior.
Summary
- C# is a modern, powerful language in the .NET ecosystem.
- C# provides strong typing and automatic memory management.
- .NET enables cross-platform C# development.
- Understanding C# opens doors to diverse application development.
Exercise
Write your first C# program that prints 'Hello, C# World!'
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, C# World!");
}
}
Exercise Tips
- Create project: dotnet new console -n HelloWorld.
- Compile and run: dotnet run.
- Add command-line arguments: dotnet run arg1 arg2 (accessed via args array).
- Use string interpolation: Console.WriteLine($"Hello, {name}!");