Why doesn't anyone accept public fields in C #?

It seems like every C # static analyzer wants to complain when it sees a public field. But why? Of course, there are cases when a public (or internal) field is enough, and it makes no sense to have a property with its get_ and set_ ? What if I know for sure that I will not redefine this field or add to it (side effects are bad, right?) - shouldn't a simple field be enough?

+27
public c # properties field
Jan 26 '09 at 17:31
source share
8 answers

Because it destroys encapsulation - thatโ€™s why most people make heavy use of accessories. However, if you think this is the right solution for your task, ignore it (which means strict complaints about encapsulation) and do what you need for your project. Do not let OO Nazis inform you of this.

+38
Jan 26 '09 at 17:33
source share

This is really about the future protecting your code. When you say (my accent):

What if I know for sure that I will not redefine the field or add this (side effects are bad, right?) - shouldn't there be a simple field enough?

This is an absolute statement, and, as we know (like most static analyzers), in life there are only two absolutes.

He's just trying to protect you from that. If this is a problem, you should be able to tell the analysis to ignore it (using attributes that depend on the analysis tool used).

+27
Jan 26 '09 at 17:35
source share

Given the fact that current C # 3.0 allows for automatic properties, the syntax of which is similar:

 public int Property {get; set;} 

the extra work required to use properties on public fields is almost zero. The fact is that you can never be completely sure that the field will not be used in another way or that the accessor will never change, and given the compromise in the work, there is no reason not to implement the property.

In any case, the analyzer complains about things that, in a high percentage (in this case, for example, 99.99% of cases), are bad programming practice ... but in any case, it just complains. Fields can be made public, and there are some extreme cases where its direct use may be warranted. Like never before, use your common sense ... but keep in mind the elemental rule for best programming practices ... Is there really good reason to break the agreement? If there then go ahead, if not, or if the answer is "it takes more work," then stick to the practice ...

+21
Jan 26 '09 at 17:45
source share

Since changing open fields later to get / install accessors will lead to code breaking. See this answer for more information.

+8
Jan 26 '09 at 17:35
source share

In general, it's nice to hide fields behind properties, even if you โ€œknow for sureโ€ that you will not override this field. Too often what you โ€œknow for sureโ€ change tomorrow. And creating a property to refer to a field is just a bit of trouble.

However, static analyzers do not replace thoughts. If you are satisfied with your design and, in your opinion, the analyzer is erroneous, then ignore or (if possible) suppress this warning in this case.

+4
Jan 26 '09 at 17:35
source share

I think the fact is that, as a rule, you do not know for sure that you will not redefine this field or add to it later. The whole point of encapsulating and hiding data is that then you can do all this without changing the public interface and then breaking up the dependent classes. If your property accessors are simply just get / sets, then they will be compiled before that, so that they are not performance issues - considering what your question should be, is there any good reason not to use them?

+1
Jan 26 '09 at 17:36
source share

Another benefit property brings to the table when performing Reflection. When you reflect on your class, you can get all the properties in one shot, instead of getting the AND properties and fields.

+1
Jan 26 '09 at 17:50
source share

And don't forget that accessors give you the flexibility to work with multiple threads.

0
Jan 26 '09 at 17:51
source share



All Articles