0
0
C Sharp (C#)programming~10 mins

Property validation logic in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Property validation logic
Start
Set Property Value
Validate Value
Valid
Assign
End
When setting a property, the value is checked. If valid, it is assigned; if not, an error is raised.
Execution Sample
C Sharp (C#)
private int age;
public int Age {
  get => age;
  set {
    if (value < 0) throw new ArgumentException("Age cannot be negative");
    age = value;
  }
}
This code sets an Age property that throws an error if a negative value is assigned.
Execution Table
StepActionValue to SetValidation ResultProperty ValueException Thrown
1Set Age to 2525Valid25None
2Set Age to -5-5Invalid25ArgumentException: Age cannot be negative
3Set Age to 00Valid0None
4Set Age to 100100Valid100None
5Set Age to -1-1Invalid100ArgumentException: Age cannot be negative
💡 Execution stops after each set attempt; invalid values throw exceptions and do not change the property.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
age0 (default)25250100100
Key Moments - 3 Insights
Why does the property value not change when an invalid value is set?
Because the validation fails and an exception is thrown before assignment, so the old value remains unchanged (see steps 2 and 5 in execution_table).
What happens if the validation condition is missing?
Without validation, any value can be assigned, including invalid ones, which can cause bugs or unexpected behavior.
Why do we throw an exception instead of just ignoring invalid values?
Throwing an exception immediately alerts the programmer or user that something is wrong, preventing silent errors and making debugging easier.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the property value after step 3?
A25
B0
C100
D-5
💡 Hint
Check the 'Property Value' column for step 3 in the execution_table.
At which step does the validation fail and an exception is thrown?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for 'Invalid' in 'Validation Result' and 'ArgumentException' in 'Exception Thrown' columns.
If we remove the validation check, what would happen when setting Age to -5?
AAn exception is still thrown
BAge remains unchanged
CAge becomes -5 without error
DThe program crashes immediately
💡 Hint
Without validation, the setter assigns the value directly (see concept_flow).
Concept Snapshot
Property validation logic in C#:
- Use a private field to store value.
- In the property setter, check the value.
- If invalid, throw an exception.
- If valid, assign to the field.
- This prevents invalid data from being stored.
Full Transcript
This example shows how property validation works in C#. When you try to set a property, the setter checks if the value is valid. If the value is negative for Age, it throws an exception and does not change the stored value. Valid values update the property normally. This helps keep data correct and prevents errors. The execution table shows each step: setting values, validation results, and exceptions if any. The variable tracker shows how the internal age field changes only on valid assignments. Key moments explain why invalid values don't change the property and why exceptions are useful. The quiz tests understanding of these steps.