C# Fundamentals: Building Your First Application
C# is a powerful, type-safe programming language developed by Microsoft. In this guide, we'll explore the fundamentals of C# and build your first application.
Getting Started with C#
C# is part of the .NET ecosystem and provides:
- Strong typing: Catch errors at compile time
- Memory management: Automatic garbage collection
- Rich ecosystem: Extensive libraries and frameworks
- Cross-platform: Runs on Windows, macOS, and Linux
Your First C# Program
Let's start with the classic "Hello World" example:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
Object-Oriented Programming
C# is an object-oriented language. Here's a simple class example:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
}
Collections and LINQ
C# provides powerful collection types and LINQ for data manipulation:
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
foreach (var number in evenNumbers)
{
Console.WriteLine(number);
}
Exception Handling
Proper error handling is crucial in C#:
try
{
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.WriteLine("This always executes");
}
Next Steps
Now that you understand the basics, explore:
- ASP.NET Core for web development
- Entity Framework for database access
- Xamarin for mobile development
- Unity for game development
C# is a versatile language that opens many doors in software development!