Static, Partial & Nested Class in C# - Learn with Example
🌟 Introduction:
Hey there, coding champ! 👋 Ever had a toolbox with different compartments for screws, nails, and hammers? That’s kind of how classes in C# work! Sometimes, you need tools that are global (like a hammer everyone uses), parts of a tool separated for convenience, or tiny compartments inside a big one. Well, Static, Partial & Nested Class in C# work just like that!
Ready to open the coding toolbox? 🧰 Let’s dive in! 🚀
🤔 What You Are Going to Learn in This Lesson:
✔️ Understand what static, partial, and nested classes are in C#
✔️ Learn their syntax and real-life use cases
✔️ See 3-4 simple code examples with clear explanations and output
✔️ Know when to use which class type in practical scenarios
🧩 1. Static Class:
🔑 What is it?
A static class in C# can’t be instantiated. It holds static members (methods, fields, etc.) that belong to the class, not individual objects. Think of it like a utility toolbox used by everyone—no need to create multiple instances!
🌍 Real-World Example:
Imagine a calculator app. You don’t need to create a new calculator object every time you add numbers. Just call the Calculator class directly!
📝 Syntax:
static class ClassName
{
static returnType MethodName() { }
}
💻 Code Example:
using System;
static class Calculator
{
public static int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main()
{
int sum = Calculator.Add(5, 10); // Directly calling static method
Console.WriteLine($"Sum: {sum}");
}
}
🔍 Output:
Sum: 15
🧠 Explanation:
- The
Calculator
class is static. - We didn’t create an object—just called
Calculator.Add()
directly. - Easy, right? No unnecessary objects floating around! 😎
🧩 2. Partial Class:
🔑 What is it?
A partial class lets you split a class into multiple files. This is super handy when working on big projects or with teams!
🌍 Real-World Example:
Imagine you’re building a robot with your friends. One person works on the robot’s arms, another on the legs. Similarly, with partial classes, you and your team can work on different parts of the same class separately! 🤖
📝 Syntax:
// File1.cs
partial class Robot
{
public void Start() => Console.WriteLine("Robot started.");
}
// File2.cs
partial class Robot
{
public void Stop() => Console.WriteLine("Robot stopped.");
}
💻 Code Example:
using System;
// RobotPart1.cs
partial class Robot
{
public void Start() => Console.WriteLine("Robot started.");
}
// RobotPart2.cs
partial class Robot
{
public void Stop() => Console.WriteLine("Robot stopped.");
}
class Program
{
static void Main()
{
Robot myRobot = new Robot();
myRobot.Start();
myRobot.Stop();
}
}
🔍 Output:
Robot started.
Robot stopped.
🧠 Explanation:
Robot
class is split into two files but works as one.- Makes large codebases more manageable.
- Perfect for teamwork and cleaner code organization! 🙌
🧩 3. Nested Class:
🔑 What is it?
A nested class is a class defined inside another class. It’s great for logically grouping classes that are only used by their containing class.
🌍 Real-World Example:
Think of a Car 🚗 that has an Engine. The engine is part of the car, not something you’d use separately. That’s a perfect use for a nested class!
📝 Syntax:
class OuterClass
{
class InnerClass
{
// Inner class members
}
}
💻 Code Example:
using System;
class Car
{
public void Drive() => Console.WriteLine("Car is driving.");
public class Engine
{
public void Start() => Console.WriteLine("Engine started.");
}
}
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.Drive();
Car.Engine myEngine = new Car.Engine(); // Accessing nested class
myEngine.Start();
}
}
🔍 Output:
Car is driving.
Engine started.
🧠 Explanation:
Engine
is insideCar
.- You access it like
Car.Engine
. - Keeps related classes bundled together—nice and neat! 😇
🚀 Conclusion:
Woohoo! 🎉 You’ve just unlocked the magic behind Static, Partial & Nested Class in C#! Each serves a unique purpose:
- Static classes for utility methods you can call directly.
- Partial classes to split big classes for better organization.
- Nested classes to group related logic neatly.
Feeling confident? You should be! You’re doing amazing—keep that momentum going! 💪
🔎 Next what?
Guess what’s next? In the upcoming lesson, you’ll explore Access Specifier in C#! 🚀 You’ll learn how to control who can access your classes and methods—like having VIP passes for your code! 🎫
Keep coding, keep smiling! 😎💻