Do I need access to private classes by properties?

I am using an instance of a private class as a state object passed to stream.BeginRead. (The class is closed to my main thread read / write class.)

public class MainClass
{
    // ...

    private class ResponseState
    {
        public IResponse response;
        public Stream stream;
        public byte[] buffer = new byte[1024];
    }
}

Access to the class is through the fields directly. Should I really provide access to the class through properties in this case, although it should only be used to save state?

It is interesting to know what others are doing.

+3
source share
5 answers

I would like - encapsulation is useful both inside the class and outside the class. By directing all access to an element through a well-known interface (i.e., Property), you give yourself the flexibility to add logic to this access later without changing the call code.

, , , , , , .

+2

#, - .

. StyleCop SA1401: FieldsMustBePrivate.

Name - MustBePrivate
CheckId - SA1401
-

# , .

, . , . .

, #, .

, , .

, , . , ( , ), .

+5

, , , .

, # 3.0 , .

, , .

+1

, . # 3.0 .

0

. . , , , . , . , " ", , , , ( , ). . , , , , , :) . , , , , . . (, , ).

I prefer to use properties by fields, since I have many MVVMs, and I need to implement INotifyPropertyChanged, which requires them. In your case, I would not worry about wrapping them in properties, it just makes senseless fat. But if it were in a class that needed property, I would have wrapped them so that things were similar in this class.

If after all this you did not wrap them, and you needed to later, this is a right-click refactor-> encapsulate field to wrap the property if you have Resharper.

0
source

Source: https://habr.com/ru/post/1783097/


All Articles