-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharp2_1.html
More file actions
108 lines (96 loc) · 6.86 KB
/
Copy pathCSharp2_1.html
File metadata and controls
108 lines (96 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<article>
<h1>C# II: Page 1 - Object-Oriented Programming (OOP): Encapsulation & Properties</h1>
<section>
<h2>The Principle of Data Hiding</h2>
<p>In our first module, we learned how to build objects with data and behavior. Now, we must learn the most important rule of professional software design: <strong>Encapsulation</strong>. Encapsulation is the practice of bundling data and methods into a single unit (a class) and <strong>restricting direct access</strong> to the internal state. In engineering terms, this is "Information Hiding." By hiding the internal details, you prevent outside code from accidentally breaking your object's data. You provide a controlled interface while keeping the implementation hidden. In C#, we achieve this through <strong>Access Modifiers</strong> and the elegant <strong>Properties</strong> system.</p>
</section>
<section>
<h2>1. Access Modifiers: The Security Gates</h2>
<p>C# provides several keywords to control who can see and modify your code members:</p>
<ul>
<li><strong>public:</strong> Accessible from anywhere. The default for methods you want others to use.</li>
<li><strong>private:</strong> Accessible only within the same class. The default for sensitive data (Fields).</li>
<li><strong>protected:</strong> Accessible within the same class and any derived classes (Inheritance).</li>
<li><strong>internal:</strong> Accessible only within the same compiled project (Assembly).</li>
</ul>
</section>
<section>
<h2>2. Properties: The C# Advantage</h2>
<p>In other languages like Java, you have to write manual <code>get_balance()</code> and <code>set_balance()</code> methods. C# makes this much easier with <strong>Properties</strong>. A property acts like a field to the user, but it's actually a specialized method that can include <strong>Validation</strong> logic.</p>
<h3>The Code implementation (C#)</h3>
<pre><code class="language-csharp">
public class BankAccount {
private double _balance; // Private field (backing store)
public double Balance {
get { return _balance; }
set {
if (value >= 0) {
_balance = value;
} else {
Console.WriteLine("Invalid balance!");
}
}
}
}
// Auto-Implemented Properties (Shorthand)
public string OwnerName { get; set; }
</code></pre>
<div class="interactive-sim">
<h3>Encapsulation Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cs2-1');
out.innerHTML = 'student.setGpa(5.0);<br>';
setTimeout(() => out.innerHTML += '>> Invalid GPA!', 800);
">Run Simulation</button>
<div id="sim-output-cs2-1" class="sim-output"></div>
</div>
</section>
<section>
<h2>3. Why Encapsulate?</h2>
<ul>
<li><strong>Data Integrity:</strong> You can ensure that a "Quantity" never becomes negative.</li>
<li><strong>Flexibility:</strong> You can change how data is stored (e.g., switching from a local list to a database) without changing a single line of code in the classes that <em>use</em> your object.</li>
<li><strong>Maintenance:</strong> Hiding complexity makes the system easier to debug and upgrade.</li>
</ul>
</section>
<section>
<h2>Visual Learning: Video Tutorials</h2>
<p>Master the security of encapsulation with these three videos:</p>
<div style="display: flex; gap: 20px; flex-wrap: wrap; margin-top: 20px;">
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>1. C# Properties & Encapsulation</strong><br>
<a href="https://www.youtube.com/watch?v=S-I_vG8vU0c" target="_blank">Watch on YouTube →</a>
<p><small>Learn the differences between fields, getters, and setters.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>2. Access Modifiers in C# Explained</strong><br>
<a href="https://www.youtube.com/watch?v=4YmO-6vV4V8" target="_blank">Watch on YouTube →</a>
<p><small>Public, Private, Protected, and Internal walkthrough.</small></p>
</div>
<div style="flex: 1; min-width: 250px; background: #eee; padding: 15px; border-radius: 8px;">
<strong>3. Auto-Implemented Properties & Best Practices</strong><br>
<a href="https://www.youtube.com/watch?v=Bj99L6pLpEc" target="_blank">Watch on YouTube →</a>
<p><small>Write cleaner code with modern C# property syntax.</small></p>
</div>
</div>
</section>
<section>
<h2>Real-World Relationship: The ATM and the Human Body</h2>
<p>Think of <strong>Encapsulation</strong> like an <strong>ATM</strong>. The bank has a huge database of money (the State), but they don't let you reach into the vault and grab cash yourself (Direct Access). Instead, they provide a screen and keypad (the Properties/Methods). You can only <code>Withdraw()</code> by following their rules and providing a PIN (Validation). Think of your <strong>Body</strong> as an encapsulated system: people can see your face and hear your voice (Public interface), but they can't directly reach in and change your heart rate or blood sugar (Private state). This design ensures that you function safely without accidental interference from the outside world.</p>
</section>
<section>
<h2>References & Additional Learning</h2>
<ul>
<li><a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties" target="_blank">Official Microsoft: C# Properties Guide</a></li>
<li><a href="https://www.baeldung.com/cs/encapsulation-properties" target="_blank">Baeldung: Encapsulation in C#</a></li>
<li><a href="https://www.w3schools.com/cs/cs_properties.php" target="_blank">W3Schools: C# Getters and Setters</a></li>
<li><a href="https://pythontutorial.net/advanced-python/python-property/" target="_blank">Property Comparison: Python vs C#</a></li>
</ul>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="CSharp1_10.html" style="text-decoration: none; color: #6c757d;">← Previous: Classes & Objects</a>
<a href="#" data-file="CSharp2_2.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Inheritance & Poly →</a>
</div>
</footer>
</article>