When does it make sense to use a public field?

后端 未结 8 1339
时光说笑
时光说笑 2021-02-18 16:52

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:26

    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.

提交回复
热议问题