internal class TestBase
{
public virtual List<int> testProp { get; }
}
internal class Test : TestBase
{
class bool filteringLessThanZeroes = false;
public override List<int> testProp
{
get => base.testProp.Where(i => filteringLessThanZeroes ? i > 0 : i >= 0).ToList(); }
}
// the Add calls here do nothing because every time I grab testProp
// it filters and returns me a new list
test.testProp.Add(0);
test.testProp.Add(23);
test.testProp.Add(22);
foreach (var item in test.testProp)
{
Console.WriteLine(item); // I should get 23, 22 but testProp is empty
}
The Add calls do nothing because every time I grab testProp it filters and returns me a new list so nothing is printed to the console.
Is there a way I can get the overridden property to somehow call parent's Add method?
Or is the entire approach wrong? If so, how would you implement something like this where when you wanna grab something it filters it out but adding stuff on it works.
I want the same property to behave differently based on the bool property as this property is being used everywhere and I don't wanna refactor that.
I can do this very easily in terms of a string property where I can for instance remove some part of text when they get it and concatenate it again when they set it.
I just need to know how to do this with lists and other objects with method calls.