C# Overview
C# (pronounced "See Sharp") is a modern, object-oriented, and type-safe programming language. C# enables developers to build many types of secure and robust applications that run in .NET.
Key Features
- Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
- Type-Safe: Prevents type errors at compile time and runtime.
- Modern Language Features: Includes properties, LINQ, async/await, pattern matching.
- Cross-Platform: Runs on Windows, Linux, and macOS via .NET Core.
- Garbage Collection: Automatic memory management.
- Rich Standard Library: Comprehensive .NET Framework Class Library.
- Interoperability: Can work with other languages in the .NET ecosystem.
Common Use Cases
Windows Applications
Used with WPF, Windows Forms, and UWP for desktop apps.
Web Development
ASP.NET Core for building web applications and APIs.
Game Development
Primary language for Unity game engine.
Enterprise Software
Used for business applications and services.
Example Code
// Modern C# example demonstrating OOP and features
using System;
namespace CSharpExample
{
class Program
{
static void Main(string[] args)
{
// Pattern matching
object greeting = "Hello, World!";
if (greeting is string message)
{
Console.WriteLine(message.ToUpper());
}
// Create a person using record (immutable)
var person = new Person("Alice", 30);
Console.WriteLine(person);
// LINQ example
var numbers = new[] { 1, 2, 3, 4, 5 };
var squares = numbers.Select(x => x * x);
Console.WriteLine(string.Join(", ", squares));
}
}
// Record type (immutable)
public record Person(string Name, int Age);
// Interface and class example
public interface IShape
{
double Area { get; }
}
public class Circle : IShape
{
public double Radius { get; }
public Circle(double radius) => Radius = radius;
public double Area => Math.PI * Radius * Radius;
}
}