Getting started
🔍 Key Differences Between ASP.NET Core & ASP.NET Core MVC
.NET Framework vs .NET Core
Setting up .NET Core Environment
Option 1: Installing Visual Studio
Option 2: Installing VS Code
Option 1: Installing Visual Studio
C#
Increment and Decrement Operators
C# is and as Operator
C# is and as Operator – Easy Guide with Examples
Lambda Expression Operator
Index and Range Operators
Checked and Unchecked Operators
Null-Coalescing Operators
Null-Conditional Operators
C# Basic Statements
Mastering C# Statements: Your Ultimate Guide!
Ever felt stuck in a C# program, wondering why your code isn’t working as expected? Maybe you’ve been scratching your head over loops, struggling with exceptions, or just feeling lost with all those keywords. Don’t worry—I’ve got you!
This tutorial is your friendly guide to understanding
C# statements, and guess what? We’ll keep it simple, fun, and super interactive. 💡
Why Should You Care About C# Statements? 🤔
Think of statements as the
building blocks of your code. They tell your program what to do and when to do it. Without them, your code is just a bunch of words sitting there, doing nothing!
Mastering these statements will:
✅ Make you a confident coder—you’ll understand what’s happening inside your program.
✅ Help you fix errors faster—goodbye, frustration!
✅ Level up your skills—whether you’re a beginner or already coding, this will make you better.
What You’re Going to Learn 📚
We’re covering
everything you need to know about C# statements:
- 🔥 var & const – Understanding implicit typing and constants.
- 🚀 this – Referring to the current instance of a class.
- 🔥 Blocks & Empty Statements – Where your code lives and when to use an empty statement.
- 🚀 Goto & Labels – Jumping between code lines (but don’t overuse it!).
- 🔄 Break & Continue – Controlling loops like a pro.
- 🎯 Return & Throw – Exiting methods and handling errors the smart way.
- 📌 Checked & Unchecked – Controlling number overflows (super useful!).
- 🔐 Lock – Handling multi-threaded code without breaking things.
- 💡 Using – Automatically cleaning up resources (like magic).
- 📊 Enums & Structures – Organizing data neatly.
- 🛡 Variable Scope – Understanding local, block, class-level, and global scope in C#.
Let’s Make This Fun! 🎉
No boring theory dumps here! Instead, you’ll get
real examples, easy explanations, and coding exercises to sharpen your skills. And hey, if you ever get stuck, just take a deep breath, grab a coffee (or tea ☕), and come back. You’ve got this!
So, Are You Ready? 🚀
Hit that next lesson and let’s start writing some awesome C# code together. Trust me, by the end of this, you’ll be coding like a champ! 🏆
Let’s do this! 💪🔥
Checked
1. Checked Statement
Alright, let’s keep it simple! Imagine you’re carrying a glass full of water. If you keep pouring more water into it, at some point, it overflows and makes a mess. Now, what if you had a magical cup that warns you before it spills? That’s exactly what a checked statement does in C#!
When you’re working with numbers in programming, sometimes they get too big and overflow—like when a water glass can’t hold more liquid. If you don’t check for overflow, the number might turn into something completely unexpected (like negative when it should be positive). That’s a disaster, right?
This is where the checked statement comes to the rescue! It monitors your numbers and if an overflow happens, it stops the program and throws an error. That way, you know something went wrong instead of letting weird numbers ruin your calculations.
Why Use a Checked Statement?
Here are some real-world reasons why you should use it:
✅ Prevents unexpected results – Without checking, a number can flip to something completely wrong.
✅ Catches mistakes early – Instead of hunting down weird bugs later, your program stops immediately when an overflow happens.
✅ Great for important calculations – If you’re dealing with money, scores, or anything critical, you don’t want errors slipping through.
✅ Makes debugging easier – You’ll immediately know where the problem is, rather than scratching your head over mysterious numbers.
When Should You Use It?
👉 When working with large numbers – If your numbers grow big fast, better be safe than sorry.
👉 When precision matters – Financial apps, game scores, or scientific calculations should always be checked.
👉 When you want safer code – It helps you avoid sneaky bugs that can break your program later.
In short, a checked statement is like a safety net—it protects your program from number overflow disasters! 🚀
Alright, imagine you’re filling a water glass again. But this time, instead of stopping when it overflows, you just keep pouring—and whatever spills, spills. No warnings, no stopping, just let it happen. That’s exactly what an unchecked statement does in C#!
When numbers get too big in programming, they overflow. Without any checks, the program just keeps running, even if the number turns into something completely unexpected. Instead of throwing an error, it silently ignores the problem, which might lead to weird results later.
Why Use an Unchecked Statement?
There are a few reasons why you might let things slide instead of stopping the program:
✅ Faster execution – Without checking for overflow, your program runs slightly quicker.
✅ You know what you’re doing – If you’re handling values that are safe, checking might be unnecessary.
✅ You don’t care about overflow – Sometimes, you just want to let numbers wrap around and move on.
When Should You Use It?
👉 When performance matters – If you’re doing a lot of number crunching and need speed, unchecked might help.
👉 When overflow isn’t a big deal – If the result doesn’t break your logic, you can ignore it.
👉 When you’re working with non-critical data – For example, in small calculations where a wrong value won’t cause a disaster.
But be careful! An unchecked statement is like driving without seat belts—it might be fine most of the time, but when things go wrong, they go really wrong. So, use it wisely! 😃
Unchecked Example (Ignores Overflow)
C# Advanced Statements
👋 Welcome to the C# Advanced Statements Chapter!
Hey there, C# enthusiast! 🎉 If you’ve mastered the basics, it’s time to level up your skills with C# advanced statements. These powerful features help you write efficient, clean, and optimized code.
In this chapter, we’ll break down complex concepts into simple, easy-to-follow lessons. Each topic is packed with practical examples, real-world use cases, and clear explanations to help you understand when and why to use them.
📚 What You Are Going to Learn
Throughout this chapter, you’ll dive deep into the following C# advanced statements:
- ✔️ Constructor & Destructor – 🏗️ Learn how objects are initialized and cleaned up.
- 🔹 sizeof – 📏 Discover how to get the memory size of data types.
- 🔰 nameof – 🔖 Use this for safe and flexible code referencing.
- 📛 Alias – 🏷️ Create alternative names for types and namespaces.
- ⏩ Async & Await – 🚀 Master asynchronous programming for responsive applications.
- 🚫 Sealed – 🔒 Prevent class inheritance and ensure security.
- 🔄 Dynamic – 🌀 Work with dynamic types without compile-time type checking.
- 🌍 Global – 🌐 Understand how global accessibility works in C#.
- ⚠️ Required – ❗ Ensure mandatory properties in C# objects.
- 🔁 Iterators – 🔃 Implement custom iteration logic in collections.
- 📌 Yield Statement – 🎯 Simplify iterator methods and enhance performance.
By the end of this chapter, you’ll have a solid grasp of these advanced features and how to use them effectively in real-world applications. 🚀
Conclusion – Why This Chapter Matters
Mastering C# advanced statements will make you a better, more efficient C# developer. These concepts will enhance your coding abilities, improve performance, and give you more control over your programs.
So, whether you’re working on a high-performance application, optimizing code, or writing cleaner and more maintainable programs, these topics are must-know essentials!
Next What?
In the next lessons, we’ll dive into Constructors & Destructors and see how objects come to life and get cleaned up in C#. You’re going to love it!
👉 Stay tuned and get ready to code like a pro! 💡🔥
Sealed
Example: Sealed Class Restricting Inheritance
Global
File 2: Person.cs (No Need to Include ‘using System’)
File 3: Program.cs (No Need to Include ‘using System’)
Conditional statements
Hey there, future C# expert! 👋 Ever wondered how your code makes decisions? Whether it’s checking a user’s login, calculating discounts, or choosing a game mode—conditional statements are everywhere!
In this chapter, you’ll learn how to control the flow of your program using:
✅ If-Else Statements – Making decisions based on conditions.
✅ Switch Case – A cleaner way to handle multiple conditions.
By the end of this, you’ll be able to write smart, efficient, and readable C# programs! 😎
What Will You Learn? 📚
🟢 If-Else Statements (The Basics of Decision Making 🤔)
You’ll start with if-else statements, the building blocks of decision-making in C#. We’ll cover:
✔️ if statement – Runs code only if a condition is true.
✔️ if-else statement – Adds an alternative option when the condition is false.
✔️ else-if statement – Checks multiple conditions.
✔️ Nested if statements – One if inside another for complex decisions.
🔵 Switch Case (For Multiple Choices 🎯)
When you have many conditions to check, switch case makes your code cleaner and easier to read. You’ll learn:
✅ Traditional Switch Statement – The classic way to handle multiple conditions. 🏛️
✅ Switch Expression (C# 8.0+) – A shorter, cleaner way to write switch cases. 📝
✅ Pattern Matching in Switch (C# 9.0+) – A smarter way to handle different types of data. 🎯
✅ Switch with When Clause – Adding extra conditions inside a switch case. 🔍
🟢 Advanced Conditional Statements:
Conditional statements in C# can be enhanced using advanced features that improve efficiency and readability. These features help developers write more concise and optimized code.
✔ && (Logical AND) and || (Logical OR) – Combining conditions.
✔ ?? (Null-Coalescing) and ?. (Null-Conditional) – Handling null values safely.
✔ goto case and goto default – Jumping between switch cases.
✔ Short-Circuit Evaluation – Optimizing performance in condition checks.
🟣 Programming Examples
– Practical examples to apply your knowledge.
🟠 Exercises
Practice problems to test your skills.
Each lesson comes with easy-to-understand examples and real-world scenarios to help you grasp these concepts faster! 🚀
Why Should You Learn This? 🤷♂️
- Make your programs smarter – Let them decide what to do!
- Avoid unnecessary code repetition – Keep it clean and efficient.
- Handle user inputs like a pro – Build dynamic applications!
If-Else and Switch Case form the foundation of every C# program, so mastering them will make you a better developer! 😎
Conclusion 🎯
Woohoo! 🎉 You’re about to level up your C# skills by mastering conditional statements. Whether it’s if-else or switch case, you’ll soon be writing code that thinks and makes decisions like a pro!
👉 Ready? Let’s dive into If-Else Statements first! 🚀
Switch Expression (C# 8.0+)
Syntax of Switch Expression in C#
Pattern Matching in switch (C# 9.0+)
Advanced Conditional Statement
C# Loop
Welcome to the Ultimate Guide on Loops in C#!
Imagine you’re making a coffee for each of your friends, one by one. Would you rather repeat the same steps for everyone, or have a way to automatically brew coffee for all? That’s exactly what loops in C# do in programming—they save you from repeating code and make your life easier!
In this series, we’ll explore different types of loops, when to use them, and how to write clean, efficient code. With real-world examples, hands-on exercises, and a friendly vibe, you’ll breeze through these lessons—even if you’re just starting out! 🚀
What You Are Going to Learn
Here’s a sneak peek at the exciting lessons ahead:
🔄 Basic Loops:
- For Loop – Repeat tasks with a counter. Perfect for when you know how many times you need to loop.
- Foreach Loop – Ideal for cycling through arrays, lists, and collections. No messy counters—just smooth iterations!
- While Loop – Keep looping until a condition changes. Great for when you don’t know how many times to repeat.
- Do-While Loop – Similar to the while loop, but it always runs at least once.
⚡ Advanced Loops:
- Parallel.For – Speed things up by running loops simultaneously. Perfect for heavy tasks and large data sets.
- Parallel.ForEach – Like
foreach, but faster! Learn how to boost performance with parallel execution. - goto Loop – Explore this lesser-used loop for special cases. Use with caution—it can get tricky! 😅
🔁 Beyond Loops:
- Recursion – A function that calls itself? Mind-blowing! Discover how recursion can solve complex problems elegantly.
💻 Hands-On Practice:
- Programming Examples – See real-world scenarios with complete code and outputs.
- Programming Exercises – Test your skills with fun, challenging problems. Hints included to keep you motivated! 😉
Why Learn Loops in C#?
Loops are essential building blocks in programming. Without them, you’d be stuck writing repetitive code (and no one wants that!). By mastering loops, you’ll:
✅ Write cleaner, more efficient code
✅ Handle large amounts of data effortlessly
✅ Save time by automating repetitive tasks
✅ Build a strong foundation for more advanced C# concepts
Who Is This For?
Whether you’re a beginner exploring programming or someone brushing up on your skills, these lessons are crafted with you in mind. We keep things simple, interactive, and fun—because learning should never be boring! 😎
Ready to Start Your Loop Journey?
👉 Begin with the For Loop Lesson →
Let’s dive in and make looping in C# your new superpower! 💪
While loop
Syntax of While Loop in C#
Methods, Classes & Objects (Basic)
Methods
📌 Basic Syntax of a Method in C#
🎯 Example 1 – A Simple Method (No Parameters, No Return Type)
🎯 Example 2 – Method with Parameters (Takes Input)
🎯 Example 3 – Method with Return Type (Gives Output Back)
Parameter Arrays (params)
Optional & Named Parameters
Best Practices and Common Mistakes
Understanding Array
✅ 1. C# Array Example: Store Student Scores.
Multi dimensional array
🚀 Looping Through the 2D Array:
Example: Initializing and Accessing a 3D Array
Passing array as parameter
Returning Arrays from Methods
Sort, Search, Reverse, and Copy Arrays
Nested & Multiple Catch
📝 Syntax for Nested Catch:
Complete system exception
Checked and unchecked statements
Introduction to File Handling
Deleting and Moving Files
Path Class
📂 Example 1: Extracting File Name and Extension
Exception Handling in File Operations
Best Practices and Common Mistakes in File Handling
🖥️ Output (if file doesn’t exist):
🖥️ Output (inside log.txt file after multiple runs):
🖥️ Output (inside transactions.log):
File Streams and Buffering
Asynchronous File Operations
File Compression and Decompression
Example 1: Compress a File Using GZipStream
Code: Compress a File
Example 2: Decompress a GZip File
Code: Decompress a File
Working with Temporary Files
File Access Permissions and Security
FileSystemWatcher - Realtime File Monitoring
Large Files and Memory Optimization
OOPs - Classes, Methods & Objects
👋 Introduction:
Hey there, coding champ! 🌟 Ever wondered how large programs stay organized and easy to manage? Well, that’s where Classes in C# and Methods in C# come to the rescue! Think of classes as blueprints for creating objects, while methods are like instructions telling those objects what to do. Pretty cool, right? 😎
Imagine you’re designing a robot. The class is the robot’s blueprint, and the methods are the robot’s actions—like walk, talk, and dance! 💃🤖 This chapter will take you on a fun journey through classes, objects, and methods with easy words, real-world examples, and lots of hands-on coding. 🚀
📖 What You Are Going to Learn in This Chapter:
Here’s what’s lined up for you:
- 🏗️ Classes in C# – Understand how to create blueprints for your objects.
- 🏠 Constructor & Destructor – Learn how objects are built and cleaned up.
- 🧩 Static, Partial & Nested Class – Explore different types of classes and their uses.
- 🛡️ Access Modifiers in Classes – Control who can access what in your code.
- 🧬 Inheritance – Reuse code by inheriting features from other classes.
- 🧸 Objects in C# – Bring your classes to life by creating objects.
- 📝 Methods in C# – Discover how to define actions for your objects.
- 🌱 Best Practices for Writing Clean Classes and Methods – Write neat and maintainable code.
- 🚫 Common Mistakes to Avoid with Classes and Methods – Steer clear of common pitfalls.
Each lesson is designed to be simple, fun, and packed with examples that make sense. Plus, you’ll get practical tips along the way. You got this! 💪
🏁 Conclusion:
By the end of this chapter, you’ll have a solid grip on Classes in C# and Methods in C#. You’ll know how to build structured programs, reuse code efficiently, and write clean, understandable code. 💻✨ Just remember: practice is the key! Even if you stumble, keep going—every coder starts somewhere. 🚀
📅 Next What?
Woohoo! 🎉 You’re about to dive deep into each topic and get your hands dirty with code. In the next lesson, we’ll kick things off with Classes in C#—the backbone of object-oriented programming. Get ready to create your first class and see how it all works in action! 🙌
Static, Partial & Nested Class
Access Modifiers in Classes
Best Practices for Writing Clean Classes and Methods
OOPs - Constructors & Destructors
Destructor (~)
🖥️ Output (May vary due to Garbage Collection timing):
Abstraction in C#
💻 Example 1 – Abstract Class in C# 🚀
Example of Interface in C# 🚗
Abstract Class vs Interface – What’s the Difference? ⚖️
🌍 Real-World Example – Online Payment System
Best Practices and Guidelines
Best Practices & Common Mistakes
1️⃣ Use Interfaces for Defining Behavior, Not Implementation
✅ Good Practice
❌ Bad Practice (Interfaces should not have fields)
❌ Bad Practice (Too Many Responsibilities)
1️⃣ Not Implementing All Methods in an Interface
Inheritance in C#
🧐 How Inheritance is Used Here
1️⃣ Creating a Parent Class (Vehicle)
2️⃣ Creating a Child Class (Car)
base, virtual, override, and new keyword
5️⃣ Full Example Showing override vs new
Access Specifiers in Inheritance
Abstract and Virtual methods
3️⃣ Abstract Method vs. Virtual Method – Key Differences
Method Overriding vs Method Hiding
✅ Hiding Example – Advanced Calculator with new
Constructors vs Inheritance
Compile-time Polymorphism (Method Overloading)
Runtime Polymorphism (Method Overriding)
📝 Syntax of Method Overriding in C#
🚀 Use virtual in the parent class and override in the child class!
Polymorphism with Interfaces
Polymorphism with Abstract Classes
📝 Syntax of Abstract Classes in C#
💻 Example 1: Polymorphism with Abstract Classes in C#
Abstract Classes in C#
⚡ Example 1: Basic Abstract Class in C#
🏆 Example 2: Abstract Class with Concrete Methods
Abstract Classes vs Interfaces
Abstract Classes with Constructors
🎯 Simple Example of Abstract Class with Constructor
Delegates in C#
📝 Step-by-Step Explanation:
2️⃣ Create a Method That Matches the Delegate Signature:
3️⃣ Assign the Method to the Delegate:
📝 Step-by-Step Explanation:
2️⃣ Create the First Method:
3️⃣ Create the Second Method:
4️⃣ Assign the First Method to the Delegate:
5️⃣ Add the Second Method (Multi-Casting):
📝 Step-by-Step Explanation:
1️⃣ Creates an Action delegate:
2️⃣ Invokes the delegate:
Multicast Delegates
Syntax of Multicast Delegates in C#
Simple Example of Multicast Delegates in C#
Another Example: Logging System 📝
Built-in Delegates
Syntax of Built-in Delegates in C#
Generic Methods in C#
✨ Example 1: A Simple Generic Method
🏗 Example 2: Swapping Two Values Using Generics
Generics List<T>
📌 Example Code Using These Methods
Generics LinkedList<T>
Example: Creating a LinkedList of Strings
Generics Dictionary<TKey, TValue>
Example: Creating a Dictionary of Student Scores
🛠️ Code Breakdown: Line by Line
1️⃣ using System; & using System.Collections.Generic;
2️⃣ Creating a Dictionary
3️⃣ Adding Key-Value Pairs (Student Names & Scores)
4️⃣ Accessing a Value from Dictionary
What Happens If the Key Doesn’t Exist?
📌 Important Methods of Generics Dictionary<TKey, TValue> in C#
💡 Example 1: Working with Dictionary Methods
Generic Queue<T>
Syntax of Generics Queue<T> in C#
2️⃣ Adding People to the Queue (Enqueue)
4️⃣ Removing the First Person (Dequeue)
5️⃣ Checking the Next Person in Line (Peek)
📌 Important Methods of Queue<T>
Generic Stack<T>
🎯 Example 1: Creating and Using a Stack<T> in C#
🔥 Important Methods of Stack<T> in C#
💡 Example 2: Checking if an Item Exists in Stack
💡 Example 3: Reversing a String Using Stack<T> in C#
Generics HashSet<T>
🚀 Example 1: Basic HashSet<T> in C#
Introduction to Threads
👎 Without Threads (Single-threaded)
👍 With Threads (Multi-threaded)
Thread Lifecycle and Management
✅ Example: Checking Thread States
🔥 Foreground vs. Background Threads in C#
🟢 Example 1: Foreground Thread (Keeps Running Until Completion)
🔵 Example 2: Background Thread (Stops When Main Thread Ends)
🎯 Key Differences Between Foreground and Background Threads
✅ Example: Manually Pausing and Resuming a Thread
✅ Checking if a Thread is Alive (IsAlive property)
✅ Example: Auto-Save with Background Thread
Thread Synchronization and Safety
Lock & Monitor
🤔 When to Use lock vs. Monitor?
Mutex, Semaphore & SemaphoreSlim
🤔 Mutex vs Semaphore vs SemaphoreSlim
AutoResetEvent and ManualResetEvent
🤔 AutoResetEvent vs ManualResetEvent – What’s the Difference?
Deadlocks and common pitfalls
Thread Pooling
🔄 Thread Pooling vs Manual Thread Creation
Multithreading
🚀 Example 1: Creating a Simple Thread in C#
✅ Basic Code Example
🎯 Example 2: Running Multiple Threads
✅ Code Example
Understanding TPL
🚀 Example 1: Creating a Simple Task
✅ Code Example
🎯 Example 2: Running Multiple Tasks in Parallel
✅ Code Example