When does it make sense to use a public field?

后端 未结 8 1238
北恋
北恋 2021-02-18 17:02

This is a question I have had for a while now:

When does it make sense to expose a field publicly like so?

public class SomeClass()
{
   public int backi         


        
8条回答
  •  无人共我
    2021-02-18 17:13

    So the best answer I can give is that when you do magic via reflection, as one is wont to do when taking advantage of a statically typed language, properties have meaning over fields. From things like ORM tools, to mappers, and to binding data, properties can have different meaning than fields from that behavioral standpoint.

    The JITter doesn't turn properties into fields, it can inline them, though I won't promise it does in all cases.

提交回复
热议问题