1

I'm concerned of people always using getter-setter pattern:

    public int MyVariable { get; private set; }

    public void SomeFunction(){
        MyVariable = 10;
    }

Which as far as I understand compiles to something like:

    private int myVariable;

    public int GetMyVariable(){
        return myVariable;
    }

    private void SetMyVariable(int value){
        myVariable = value;
    }

    public void SomeFunction()
    {
        SetMyVariable(10);
    }

Doesn't it impact the program's performance if used frequently? Isn't it better to do like that:

    private int myVariable;
    public int MyVariable { get {return myVariable; } }

    public void SomeFunction(){
        myVariable = 10;
    }
Mike Trusov
  • 1,958
  • 1
  • 15
  • 23
Georgii Oleinikov
  • 3,865
  • 3
  • 27
  • 27

3 Answers3

5

First off, this type of optimization is typically counter productive - trying to optimize out single method calls is something that will typically have no impact in any real world, measured performance. You're likely better off focusing your efforts on optimizing the algorithms you use at a higher level, and not micro-optimizing the language features used.

Doesn't it impact the program's performance if used frequently? Isn't it better to do like that:

No. This will be effectively the same once compiled.

In addition, in a release build, the JIT compiler will typically completely optimize away the get and set methods, inlining them completely. This will effectively make it perform exactly like using a public field, and have the same performance as a direct member access.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Exactly. Using `get` and `set` is the __best__ policy, even if they are just mindless `return xxx` and `this.xxx = value` statements. The point is that if you ever need to change the behavior/validation of these settings, you can do it in __one__ place (in the `get/set`) instead of every place you access it. Can you imagine for some property used in 100+ locations needing to be "checked for bounds" without doing that at the property level? Ugh. – Erik Jan 22 '13 at 01:09
3

Its the same thing when it's compiled.

For proof you can check out this link. Here is an excerpt from it:

Notice how the get and set accessors in Listing 10-5 do not have implementations. In an auto-implemented property, the C# compiler creates the backing store field behind the scenes, giving the same logic that exists with traditional properties, but saving you from having to use all of the syntax of the traditional property.

FrostyFire
  • 3,212
  • 3
  • 29
  • 53
1

It depends on the compiler. Yes, it may impact the performance. For example, if you are developing a game with Unity Engine properties will be likely not converted to fields automatically, you can inspect produced IL2CPP code and find their C++ wrappers. So, you can win a little if you will use fields instead in render and physics loops. If you have huge amount of such properties access you probably will not find any of them alone in profiler so it will be hard to optimise in the future if all your huge codebase has a lot of properties in critical places because their impact will be distributed evenly. So, I prefer to make things more optimal in critical places right in the development time not delaying them to profiling times. But it is OK to have properties in less critical places.

dmitry1100
  • 1,199
  • 12
  • 26