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.
1// Example: Your first C# program
2using System;
3
4class Program
5{
6 static void Main()
7 {
8 Console.WriteLine("Hello, C# Programming!");
9 }
10}
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.
1# Example: Running a C# program
2dotnet 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
.
1// Example: Declaring variables
2using System;
3
4class Program
5{
6 static void Main()
7 {
8 int age = 25;
9 double height = 5.9;
10 string name = "Alice";
11 bool isStudent = true;
12
13 Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}");
14 }
15}
Control Flow
C# supports if
, else
, switch
, and loops (for
, while
, do-while
) for controlling program flow.
1// Example: Conditional statement
2using System;
3
4class Program
5{
6 static void Main()
7 {
8 int age = 18;
9
10 if (age >= 18)
11 {
12 Console.WriteLine("You are an adult.");
13 }
14 else
15 {
16 Console.WriteLine("You are a minor.");
17 }
18 }
19}
Functions
Functions (or methods) are defined using the void
or return_type
keyword. C# supports both static and instance methods.
1// Example: Function
2using System;
3
4class Program
5{
6 static int Add(int a, int b)
7 {
8 return a + b;
9 }
10
11 static void Main()
12 {
13 int result = Add(5, 10);
14 Console.WriteLine($"Result: {result}"); // 15
15 }
16}
Arrays and Collections
C# provides arrays and collections like List
, Dictionary
, and HashSet
for storing and manipulating data.
1// Example: List
2using System;
3using System.Collections.Generic;
4
5class Program
6{
7 static void Main()
8 {
9 List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
10
11 foreach (int number in numbers)
12 {
13 Console.WriteLine($"Number: {number}");
14 }
15 }
16}
Object-Oriented Programming
C# is an object-oriented language, supporting classes, inheritance, polymorphism, and encapsulation.
1// Example: Class and Object
2using System;
3
4class Person
5{
6 public string Name { get; set; }
7 public int Age { get; set; }
8
9 public void Display()
10 {
11 Console.WriteLine($"Name: {Name}, Age: {Age}");
12 }
13}
14
15class Program
16{
17 static void Main()
18 {
19 Person person = new Person { Name = "Alice", Age = 25 };
20 person.Display();
21 }
22}
Exception Handling
C# uses try
, catch
, and finally
blocks to handle exceptions gracefully.
1// Example: Exception handling
2using System;
3
4class Program
5{
6 static void Main()
7 {
8 try
9 {
10 int result = 10 / 0;
11 }
12 catch (DivideByZeroException ex)
13 {
14 Console.WriteLine($"Error: {ex.Message}");
15 }
16 finally
17 {
18 Console.WriteLine("Execution complete.");
19 }
20 }
21}
LINQ
Language Integrated Query (LINQ) allows you to query collections and databases in a readable and expressive way.
1// Example: LINQ
2using System;
3using System.Linq;
4using System.Collections.Generic;
5
6class Program
7{
8 static void Main()
9 {
10 List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
11 var evenNumbers = numbers.Where(n => n % 2 == 0);
12
13 foreach (int number in evenNumbers)
14 {
15 Console.WriteLine($"Even Number: {number}");
16 }
17 }
18}
Asynchronous Programming
C# supports asynchronous programming using async
and await
keywords, making it easy to write non-blocking code.
1// Example: Asynchronous programming
2using System;
3using System.Threading.Tasks;
4
5class Program
6{
7 static async Task Main()
8 {
9 Console.WriteLine("Starting task...");
10 await Task.Delay(2000); // Simulate a 2-second delay
11 Console.WriteLine("Task completed.");
12 }
13}