Introduction to Programming in C#
C# (pronounced “C Sharp”) is a modern, object-oriented programming language developed by Microsoft. It is widely used for building Windows applications, web applications, games (via Unity), and more. C# is part of the .NET ecosystem, which provides a rich set of libraries and tools for developers. Its simplicity, versatility, and strong typing make it a great choice for both beginners and experienced programmers.
This guide will introduce you to the fundamentals of C# programming, helping you write clean, efficient, and maintainable code.
Table of Contents
- What is C#?
- Setting Up Your Environment
- Variables and Data Types
- Control Flow
- Functions
- Arrays and Collections
- Object-Oriented Programming
- Exception Handling
- LINQ
- Asynchronous Programming
What is C#?
C# is a general-purpose, object-oriented programming language designed for the .NET platform. It is used for building a wide range of applications, from desktop to web and mobile.
// Example: Your first C# program
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, C# Programming!");
}
}
Setting Up Your Environment
To write and run C# programs, you need the .NET SDK installed on your system. You can download it from the official .NET website.
# Example: Running a C# program
dotnet run
Variables and Data Types
C# is statically typed, meaning variable types are determined at compile time. Common data types include int
, float
, double
, string
, and bool
.
// Example: Declaring variables
using System;
class Program
{
static void Main()
{
int age = 25;
double height = 5.9;
string name = "Alice";
bool isStudent = true;
Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}");
}
}
Control Flow
C# supports if
, else
, switch
, and loops (for
, while
, do-while
) for controlling program flow.
// Example: Conditional statement
using System;
class Program
{
static void Main()
{
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are a minor.");
}
}
}
Functions
Functions (or methods) are defined using the void
or return_type
keyword. C# supports both static and instance methods.
// Example: Function
using System;
class Program
{
static int Add(int a, int b)
{
return a + b;
}
static void Main()
{
int result = Add(5, 10);
Console.WriteLine($"Result: {result}"); // 15
}
}
Arrays and Collections
C# provides arrays and collections like List
, Dictionary
, and HashSet
for storing and manipulating data.
// Example: List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine($"Number: {number}");
}
}
}
Object-Oriented Programming
C# is an object-oriented language, supporting classes, inheritance, polymorphism, and encapsulation.
// Example: Class and Object
using System;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Display()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
class Program
{
static void Main()
{
Person person = new Person { Name = "Alice", Age = 25 };
person.Display();
}
}
Exception Handling
C# uses try
, catch
, and finally
blocks to handle exceptions gracefully.
// Example: Exception handling
using System;
class Program
{
static void Main()
{
try
{
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.WriteLine("Execution complete.");
}
}
}
LINQ
Language Integrated Query (LINQ) allows you to query collections and databases in a readable and expressive way.
// Example: LINQ
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (int number in evenNumbers)
{
Console.WriteLine($"Even Number: {number}");
}
}
}
Asynchronous Programming
C# supports asynchronous programming using async
and await
keywords, making it easy to write non-blocking code.
// Example: Asynchronous programming
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
Console.WriteLine("Starting task...");
await Task.Delay(2000); // Simulate a 2-second delay
Console.WriteLine("Task completed.");
}
}