0 day streak
csharp / beginner
Snippet

Sensitive Field Encapsulation

Encapsulation hides sensitive data within a class, preventing external code from directly accessing or corrupting critical information.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
public class UserAccount
{
private string _passwordHash;
public void UpdatePassword(string hash)
{
// Internal logic to safely update data
if (!string.IsNullOrEmpty(hash)) _passwordHash = hash;
}
}
Breakdown
1
private string _passwordHash
Restricts access to the variable within the class only.
2
UpdatePassword
A controlled method to modify the private state.