Performance overhead for properties in .NET

前端 未结 9 1922
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 01:43

I read somewhere that having public properties is preferable to having public members in a class.

  1. Is this only because of abstaraction and modularity? Are

9条回答
  •  不思量自难忘°
    2020-12-10 02:07

    1) Its for encapsulation principles, but other .NET features use properties such as data binding.

    2) I'm not sure I agree with that, I've always heard that if the property is a straight get/set its just as fast as a standard field access - the compiler does this for you.

    Update: seems to be a bit of both, compiles to method call but JIT-optimized away. Either way, this sort of performance concern is not going to have a meaningful impact on your code. However, note that the guidance around implementing properties is to make them as light-weight as possible, they are not expected by callers to be expensive.

提交回复
热议问题