Get Set in C# β Master Access Specifiers Easily!
Hey there, coder! π Have you ever wondered how to control access to your class properties in C#? π€
Thatβs exactly where Get Set in C# comes into play! It helps you encapsulate data and ensure security and control over how properties are accessed and modified.
By the end of this lesson, youβll be a pro at using Get and Set accessors in C# with real-world examples and hands-on coding! So, grab your coffee β, and letβs dive in! π
π What You Will Learn in This Lesson
βοΈ What Get Set in C# means and why itβs important.
βοΈ How Get and Set accessors work in C#.
βοΈ The difference between Get Set in C# and Destructors (~).
βοΈ The syntax and real-world usage.
βοΈ Multiple coding examples with explanations and outputs.
βοΈ Why it is needed and where it is used in the real world.
π€ What is Get Set in C#?
Get and Set are accessors used to read and modify private fields inside a class. This is part of Encapsulation, which helps in hiding sensitive data and only allowing controlled access.
Letβs compare Get Set in C# with Destructors (~) to understand the difference:
Feature | Get Set in C# | Destructors (~) |
---|---|---|
Purpose | Controls property access | Cleans up resources |
When Used? | During object lifecycle | When an object is destroyed |
Manual Control? | Yes | No (handled by Garbage Collector) |
Example | Setting a name for a student | Releasing database connections |
Now that you get the idea, letβs look at the syntax! π
Β
π οΈ Syntax of Get Set in C#
class ClassName
{
private datatype variableName; // Private field
public datatype PropertyName
{
get { return variableName; } // Getter
set { variableName = value; } // Setter
}
}
get
β Used to retrieve the value of a private field.set
β Used to assign a value to the private field.
π‘ Example 1: Basic Get Set in C#
Letβs see a simple example where we set and get a person’s name!
using System;
class Person
{
private string name; // Private field
public string Name
{
get { return name; } // Getter
set { name = value; } // Setter
}
}
class Program
{
static void Main()
{
Person p = new Person();
p.Name = "Alice"; // Using Set
Console.WriteLine("Hello, " + p.Name); // Using Get
}
}
π Output:
Hello, Alice
π οΈ Code Explanation:
β
We created a private field name
.
β
Used get to retrieve the value.
β
Used set to assign a value.
β
Set Name = "Alice"
and printed it.
Nice and easy, right? Now, letβs add some validation!
π Example 2: Adding Validation with Set
We donβt want anyone to set an empty name! Letβs fix that.
using System;
class Person
{
private string name;
public string Name
{
get { return name; }
set
{
if (string.IsNullOrWhiteSpace(value))
Console.WriteLine("Name cannot be empty!");
else
name = value;
}
}
}
class Program
{
static void Main()
{
Person p = new Person();
p.Name = ""; // Invalid input
p.Name = "Bob"; // Valid input
Console.WriteLine("Hello, " + p.Name);
}
}
π Output:
Name cannot be empty!
Hello, Bob
β Now, if someone tries to set an empty name, it wonβt work! π
π Real-World Example β Bank Account System
Imagine a Bank Account where we must ensure the balance cannot be negative. Letβs use Get Set in C# to enforce this rule!
using System;
class BankAccount
{
private double balance;
public double Balance
{
get { return balance; } // Get balance
set
{
if (value >= 0)
balance = value; // Set new balance
else
Console.WriteLine("Balance cannot be negative!");
}
}
}
class Program
{
static void Main()
{
BankAccount account = new BankAccount();
account.Balance = 500; // Valid balance
Console.WriteLine("Current Balance: " + account.Balance);
account.Balance = -100; // Invalid balance
}
}
π Output:
Current Balance: 500
Balance cannot be negative!
β This is why Get Set in C# is super useful! It ensures data integrity while keeping things secure and flexible.
π Auto-Implemented Properties (Shorter Way!)
C# allows a shortcut for properties without manually defining a private field.
class Car
{
public string Model { get; set; } // Auto-implemented property
}
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.Model = "Tesla";
Console.WriteLine("Car Model: " + myCar.Model);
}
}
π Output:
Car Model: Tesla
β This saves time and keeps your code clean!
π― Why is Get Set in C# Important?
β
Encapsulation β Keeps fields private but still accessible.
β
Validation β Prevents invalid data entry.
β
Flexibility β You can modify property behavior without affecting the class.
Β
β¨ Conclusions
β
Get Set in C# helps you control how data is accessed.
β
It prevents invalid data and ensures security.
β
Real-world scenarios like banking and user data handling use this concept every day!
β
Itβs way different from Destructors (~), which clean up resources instead of managing data.
Β
Next What? π€
You’re now a pro at Get Set in C#! π But whatβs next?
In the next chapter, weβll explore Abstraction in C# β a powerful way to hide complex details and focus on what matters! Stay tuned! π
Now, go ahead and practice some Get Set magic in your code! π»β¨