Differences between Private Fields and Private Properties

后端 未结 6 1795
执笔经年
执笔经年 2020-12-15 15:33

What is the difference between using Private Properties instead of Private Fields

private String MyValue { get; set; }

// instead of

private String _myValu         


        
6条回答
  •  生来不讨喜
    2020-12-15 16:21

    When dealing with private access, the differences are very small. Yes, there is a performance hit (which may be optimized by the JIT) send properties represent a method call, instead of a direct address access.

    The main advantage to using properties is to allow changing the implementation without changing the external signature required. Since these are privately accessed, any changes to the implementation only effects local code.

    When dealing with private members, I see no advantage gained from properties, except for your team's conventions.

提交回复
热议问题