๐ ๏ธ C# Tutorial โ The Complete Beginner’s Guide to Learn Modern C#
๐ Introduction to C# Tutorial
๐น What is C#?
C# (pronounced C-Sharp) is a modern, object-oriented programming language developed by Microsoft. It runs on the .NET framework and is used to build apps for desktop, web, mobile, and games (especially with Unity ๐ฎ).
๐ Brief History of C#
Created by Anders Hejlsberg and introduced in 2000, C# has evolved with powerful features such as:
- LINQ
- async/await
- pattern matching
- and much more
๐ค Why Learn C#?
โ๏ธ Cross-platform development
โ๏ธ Strong typing with great performance
โ๏ธ Full Microsoft & .NET ecosystem support
๐ ๏ธ Setting Up Your Environment
๐พ Installing Visual Studio
- Visit visualstudio.microsoft.com
- Download Community Edition
- Choose the โ.NET desktop developmentโ workload
- Install and launch!
๐ Creating Your First C# Project
๐ File โ New โ Project โ Console App (.NET Core)
๐ Name it โ โ
Click Create โ ๐ง Start coding!
๐ค C# Basics
๐งฑ Syntax and Structure
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
๐ฆ Variables and Data Types
int age = 25;
string name = "Alice";
bool isStudent = true;
โ Operators in C#
- Arithmetic:
+,-,*,/ - Comparison:
==,!=,>,< - Logical:
&&,||
๐ Type Conversion
int x = 10;
double y = x; // Implicit
int z = (int)y; // Explicit
๐ Control Flow in C#
๐ Conditional Statements
if (x > 0) Console.WriteLine("Positive");
else if (x < 0) Console.WriteLine("Negative");
else Console.WriteLine("Zero");
๐ Loops
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
Also use: while, do-while, foreach
๐ง Methods and Functions
๐จ Defining & Calling Methods
void Greet() {
Console.WriteLine("Welcome!");
}
๐ฅ Method Parameters & Return Types
int Add(int a, int b) {
return a + b;
}
๐๏ธ Object-Oriented Programming (OOP)
๐๏ธ Classes & Objects
class Car {
public string Model;
}
Car myCar = new Car();
myCar.Model = "Tesla";
๐งฌ Inheritance
class Animal {
public void Speak() {
Console.WriteLine("Generic sound");
}
}
class Dog : Animal {}
๐ญ Polymorphism
class Animal {
public virtual void Speak() {
Console.WriteLine("Generic sound");
}
}
class Cat : Animal {
public override void Speak() {
Console.WriteLine("Meow");
}
}
๐ Encapsulation & Abstraction
- Use
private,public,protectedto restrict access - Show only what’s necessary to the outside world
๐ก๏ธ Exception Handling
โ ๏ธ Try, Catch, Finally
try {
int a = 10 / 0;
} catch (DivideByZeroException) {
Console.WriteLine("Can't divide by zero");
} finally {
Console.WriteLine("Operation complete.");
}
๐ซ Throwing Exceptions
throw new ArgumentException("Invalid input!");
๐๏ธ Collections & Arrays
๐ Arrays
int[] numbers = {1, 2, 3, 4};
๐ Lists & Dictionaries
List<string> names = new List<string>();
Dictionary<string, int> ageMap = new Dictionary<string, int>();
๐ File Handling in C#
๐ Reading and Writing Files
File.WriteAllText("log.txt", "Logging data...");
string content = File.ReadAllText("log.txt");
Console.WriteLine(content);
๐ Working with LINQ
๐ Intro to LINQ
Query data collections using SQL-like syntax.
๐ LINQ Query Example
var result = from n in numbers where n > 2 select n;
โก Asynchronous Programming
๐ async/await
async Task LoadAsync() {
await Task.Delay(1000);
Console.WriteLine("Loaded data!");
}
๐พ Working with Databases
๐งฉ Connecting to SQL Server
Use SqlConnection and SqlCommand from System.Data.SqlClient.
๐ Querying the Database
SqlCommand cmd = new SqlCommand("SELECT * FROM Users", connection);
SqlDataReader reader = cmd.ExecuteReader();
๐งฎ Creating a Simple C# App
๐ง Console Calculator
int a = 5, b = 3;
Console.WriteLine("Sum: " + (a + b));
๐ Best Practices in C# Programming
โ๏ธ Use meaningful variable and method names
โ๏ธ Keep functions short and readable
โ๏ธ Always handle exceptions
โ๏ธ Follow naming conventions (PascalCase, camelCase)
โ๏ธ Use comments sparingly but clearly
โ Conclusion
C# is beginner-friendly yet incredibly powerful. Whether youโre building enterprise apps, Unity games, or Azure cloud servicesโC# is a reliable and versatile choice.
๐ Keep coding, keep building projects, and C# mastery will follow.
โ FAQs
โ Is C# good for beginners?
โ Yes. Itโs structured, readable, and widely supported.
โ Can I build mobile apps with C#?
โ Absolutely! Use Xamarin or .NET MAUI.
โ Whatโs the difference between C# and C++?
โ C# is higher-level and managed; C++ is low-level with more manual memory management.
โ Is C# used in game development?
โ Yes, itโs the primary language for Unity.
โ Where can I practice C# online?
โ Try HackerRank, LeetCode, or Microsoftโs Learn Portal.
Share Now :
