Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Property Encapsulation

C# properties are one of the most expressive tools I've found in a c family language. My question is why can't the "backing" field be within the property block? What reasoning is this not allowed?

public int Salary //Compile error
{
    int salary = 1000;
    get {} //custom use of salary
    set {} 
}

I find it rather clunky to do the following

int salary = 1000; //poor encapsulation but working code

public int Salary
{
    get {} //edit the private field salary
    set {} 
}

I've done some optimization of some code by memozing properties using the following pattern

int field;
bool flag = false;
public int Field 
{
    get
    {
        if (flag)
            return field;
        flag = true;
        return field = expensiveOperation();
    }
}

I would really like to move these variables in the scope of the property since they have no reason to be in the scope of the instance

public int Field //compile error
{
    int field;
    bool flag = false;
    get
    {
        if (flag)
            return field;
        flag = true;
        return field = expensiveOperation();
    }
}
like image 963
t3dodson Avatar asked Jun 11 '26 15:06

t3dodson


1 Answers

If you have property, which work with its own data, not related to object where property sits, then I think you should extract that property from your class. Intent of properties is to work with data which are related to class where property is declared, and have scope of whole class.

Also keep in mind - property is just a syntax sugar for pair of methods.


Side note: you can use Lazy<T> for lazy initialization of field. E.g.

Lazy<int> field = new Lazy<int>(() => expensiveOperation());
like image 86
Sergey Berezovskiy Avatar answered Jun 13 '26 05:06

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!