OOP Concepts in .NET
<style>
body {
font-family: 'Segoe UI', sans-serif;
color: #333;
}
h1 {
line-height: 1.6;
}
h1, h2, h3 {
color: #2c3e50;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
margin: 0.3rem 0;
}
nav a {
text-decoration: none;
color: #007acc;
}
code {
background-color: #eee;
padding: 2px 4px;
border-radius: 4px;
font-family: Consolas, monospace;
}
pre {
background: #f4f4f4;
padding: 1rem;
overflow-x: auto;
border-left: 4px solid #007acc;
}
table {
width: 100%;
border-collapse: collapse;
margin: 1rem 0;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background: #007acc;
color: #fff;
}
.summary, .cta {
background: #e6f7ff;
padding: 1rem;
border: 1px solid #b3e0ff;
border-radius: 6px;
margin-top: 2rem;
}
</style>
<h1>OOP Concepts in .NET</h1>
<p>
Object-Oriented Programming (OOP) is the foundation of modern .NET application development. In this guide, you’ll explore core OOP principles—like inheritance, encapsulation, polymorphism, and abstraction—and how .NET enhances these with features like interfaces, delegates, generics, LINQ, and more.
</p>
<nav>
<h2>📌 Table of Contents</h2>
<ul>
<li><a href="#inheritance">1. Inheritance</a></li>
<li><a href="#abstraction">2. Abstraction</a></li>
<li><a href="#encapsulation">3. Encapsulation</a></li>
<li><a href="#polymorphism">4. Polymorphism</a></li>
<li><a href="#interfaces">5. Interfaces</a></li>
<li><a href="#composition">6. Composition</a></li>
<li><a href="#delegates">7. Delegates & Events</a></li>
<li><a href="#generics">8. Generics</a></li>
<li><a href="#dotnetfeatures">Other .NET Features</a></li>
<li><a href="#summary">Conclusion</a></li>
</ul>
</nav>
<section id="inheritance">
<h2>🔷 1. Inheritance</h2>
<p>Allows one class (child) to inherit fields and methods from another (parent), promoting code reuse.</p>
<ul>
<li>Supports single, hierarchical, and multilevel inheritance</li>
<li>.NET supports multiple interface inheritance</li>
<li>Use <code>virtual</code>, <code>override</code>, and <code>base</code> for method overriding</li>
</ul>
<pre><code>class Vehicle {
public void StartEngine() => Console.WriteLine("Engine started.");
}
class Car : Vehicle {
public void PlayMusic() => Console.WriteLine("Playing music...");
}
Car myCar = new Car();
myCar.StartEngine();
myCar.PlayMusic();</code></pre>
</section>
<section id="abstraction">
<h2>🔷 2. Abstraction</h2>
<p>Hides internal complexity and exposes only essential operations.</p>
<ul>
<li>Use <code>abstract</code> classes and <code>interfaces</code></li>
<li>Interfaces provide full abstraction (no implementation)</li>
</ul>
<pre><code>abstract class Vehicle {
public abstract void StartEngine();
public void StopEngine() => Console.WriteLine("Engine stopped.");
}
class Car : Vehicle {
public override void StartEngine() => Console.WriteLine("Car engine started.");
}</code></pre>
</section>
<section id="encapsulation">
<h2>🔷 3. Encapsulation</h2>
<p>Protects internal state using access modifiers and controlled access.</p>
<ul>
<li>Use <code>private</code> fields with <code>public</code> properties</li>
<li>Ensures data integrity</li>
</ul>
<pre><code>class Account {
private decimal balance;
public decimal Balance {
get { return balance; }
private set { balance = value; }
}
public void Deposit(decimal amount) {
if (amount > 0) balance += amount;
}
}</code></pre>
</section>
<section id="polymorphism">
<h2>🔷 4. Polymorphism</h2>
<p>Allows objects to behave differently based on their type.</p>
<ul>
<li><strong>Compile-time:</strong> Method overloading, operator overloading</li>
<li><strong>Runtime:</strong> Method overriding via <code>virtual</code>/<code>override</code></li>
</ul>
<pre><code>class Animal {
public virtual void Speak() => Console.WriteLine("Animal sound");
}
class Dog : Animal {
public override void Speak() => Console.WriteLine("Bark");
}
class Cat : Animal {
public override void Speak() => Console.WriteLine("Meow");
}
Animal myDog = new Dog();
myDog.Speak(); // Bark</code></pre>
</section>
<section class="summary">
<h2>📋 OOP Concepts Summary</h2>
<table>
<thead>
<tr>
<th>Concept</th><th>Purpose</th><th>Implementation</th><th>Benefits</th>
</tr>
</thead>
<tbody>
<tr><td>Inheritance</td><td>Code reuse</td><td>Derived classes</td><td>Maintainability</td></tr>
<tr><td>Abstraction</td><td>Hide complexity</td><td>Interfaces/Abstract classes</td><td>Simplified interfaces</td></tr>
<tr><td>Encapsulation</td><td>Protect state</td><td>Access modifiers, properties</td><td>Security, integrity</td></tr>
<tr><td>Polymorphism</td><td>Dynamic behavior</td><td>Overloading, overriding</td><td>Flexibility</td></tr>
</tbody>
</table>
</section>
<section id="interfaces">
<h2>🔧 5. Interfaces</h2>
<p>Defines a contract that classes implement. All members are abstract by default.</p>
<pre><code>interface IDriveable {
void Drive();
}
class Car : IDriveable {
public void Drive() => Console.WriteLine("Driving the car.");
}</code></pre>
</section>
<section id="composition">
<h2>🔧 6. Composition</h2>
<p>Favor using composed objects over inheritance for flexibility.</p>
<pre><code>class Engine {
public void Start() => Console.WriteLine("Engine started.");
}
class Car {
private Engine engine = new Engine();
public void StartCar() => engine.Start();
}</code></pre>
</section>
<section id="delegates">
<h2>🔧 7. Delegates and Events</h2>
<p>Enables callback and publish-subscribe patterns.</p>
<pre><code>public delegate void Notify();
class Process {
public event Notify ProcessCompleted;
public void StartProcess() {
Console.WriteLine("Process started.");
ProcessCompleted?.Invoke();
}
}</code></pre>
</section>
<section id="generics">
<h2>🔧 8. Generics</h2>
<p>Define type-safe and reusable data structures and methods.</p>
<pre><code>class GenericList<T> {
private T[] elements;
}</code></pre>
</section>
<section id="dotnetfeatures">
<h2>⚙️ Other .NET Features</h2>
<h3>Exception Handling</h3>
<pre><code>try {
int x = 0;
int y = 5 / x;
} catch (DivideByZeroException ex) {
Console.WriteLine("Cannot divide by zero.");
}</code></pre>
<h3>Namespaces</h3>
<pre><code>namespace Vehicles {
class Car { }
}</code></pre>
<h3>Attributes</h3>
<pre><code>[Obsolete("Use NewMethod instead")]
void OldMethod() { }</code></pre>
<h3>LINQ</h3>
<pre><code>var adults = people.Where(p => p.Age >= 18);</code></pre>
<h3>Async/Await</h3>
<pre><code>async Task<int> GetDataAsync() {
await Task.Delay(1000);
return 42;
}</code></pre>
<h3>Dependency Injection</h3>
<pre><code>public class CarService {
private readonly IEngine _engine;
public CarService(IEngine engine) {
_engine = engine;
}
}</code></pre>
<h3>Lambda Expressions</h3>
<pre><code>Func<int, int> square = x => x * x;</code></pre>
<h3>Partial Classes</h3>
<pre><code>partial class MyClass { void Method1() { } }
partial class MyClass { void Method2() { } }</code></pre>
<h3>Extension Methods</h3>
<pre><code>public static class StringExtensions {
public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str);
}</code></pre>
<h3>Records</h3>
<pre><code>public record Person(string Name, int Age);</code></pre>
<h3>Pattern Matching</h3>
<pre><code>if (obj is int number && number > 0) {
Console.WriteLine(number);
}</code></pre>
<h3>Memory Management</h3>
<pre><code>using (var file = new StreamReader("file.txt")) {
// Use file
}</code></pre>
</section>
<section class="summary">
<h2>📋 Summary of .NET Key Features</h2>
<table>
<thead>
<tr>
<th>Feature</th><th>Purpose</th><th>Benefit</th>
</tr>
</thead>
<tbody>
<tr><td>Attributes</td><td>Metadata for classes</td><td>Custom behaviors</td></tr>
<tr><td>LINQ</td><td>Query data</td><td>Readable logic</td></tr>
<tr><td>Async/Await</td><td>Async operations</td><td>Responsive UI</td></tr>
<tr><td>DI</td><td>Inject dependencies</td><td>Testable, maintainable</td></tr>
<tr><td>Lambdas</td><td>Inline functions</td><td>Concise syntax</td></tr>
<tr><td>Extension Methods</td><td>Add to existing types</td><td>No inheritance needed</td></tr>
<tr><td>Records</td><td>Immutable data</td><td>Simplified models</td></tr>
<tr><td>Pattern Matching</td><td>Conditional checks</td><td>Cleaner syntax</td></tr>
<tr><td>Memory Management</td><td>Dispose resources</td><td>Avoid memory leaks</td></tr>
</tbody>
</table>
</section>
<section id="summary" class="cta">
<h2>🎯 Conclusion</h2>
<p>
Mastering OOP and .NET features is crucial for building clean, efficient, and maintainable applications. These principles form the backbone of real-world enterprise development and prepare you for advanced design patterns and architecture in C#.
</p>
<p>📚 <strong>Next Read:</strong></p>
<ul>
<li><a href="#">SOLID Principles in C#</a></li>
<li><a href="#">Design Patterns in .NET</a></li>
<li><a href="#">Entity Framework Core Deep Dive</a></li>
</ul>
</section>
Comments
Post a Comment