Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effects of variable scope on performance? (C#)

Assume that we have 3 classes:

Place, Contact, PhoneNumber classes.

Under the Place class, I want to have a Contact class but it is better to keep PhoneNumber class under the Contact class. So in the Place class, it is more logical to reach the PhoneNumber by first getting the Contact object, then the PhoneNumber under the Contact object.

If I often need to get the PhoneNumber object from a place object, does keeping the PhoneNumber class under the Contact class (Place.Contact.PhoneNumber) instead of directly insert that object under the Place (Place.Contact) class cause any performance issues?

Why I ask this question is that these kind of scope issues have lots of performance effects in JavaScript. Does it worth to be so paranoiac about variable scope - performance relations in C#?

Thank you.

like image 931
Eren Avatar asked Dec 01 '25 19:12

Eren


1 Answers

In C# you won't see many performance issues around trivial* property getters and setters like this. However, without profiling, it is impossible to say if this will be a problem for you.

For most cases though, object graph constructions never create performance problems in C# like they can in JavaScript.


* Properties that simply return a reference to an existing object and have no additional logic.

like image 136
Andrew Hare Avatar answered Dec 04 '25 07:12

Andrew Hare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!