Is the C# compiler smart enough to optimize this code?

前端 未结 6 824
无人及你
无人及你 2020-11-27 18:22

Please ignore code readability in this question.

In terms of performance, should the following code be written like this:

int maxResults = criteria.M         


        
6条回答
  •  囚心锁ツ
    2020-11-27 19:04

    If MaxResults is a property then no, it will not optimize it, because the getter may have complex logic, say:

    private int _maxResults;
    public int MaxReuslts {
      get { return _maxResults++; }
      set { _maxResults = value; }
    }
    

    See how the behavior would change if it in-lines your code?

    If there's no logic...either method you wrote is fine, it's a very minute difference and all about how readable it is TO YOU (or your team)...you're the one looking at it.

提交回复
热议问题