MaT
MaT

Reputation: 1606

Optimization about Singleton

I am using Unity with c#. I have two objects and one needs to call a function from the other which is a Singleton. For this I have two solutions. But what's the best thing to do :
- Call for :
MyScript.Instance.MyFunc();
- Store my Singleton object in my calling object and call it like this :
myScript.MyFunc();

What is the best thing to do in term of performance and optimizations.

Thanks a lot.

Upvotes: 0

Views: 265

Answers (2)

user652038
user652038

Reputation:

MyScript.MyFunc();

The class should handle the instance internally, and it should be private. You don't need the "Instance" clutter everywhere, and nobody else needs to know about it.

Upvotes: 0

Vasiliy
Vasiliy

Reputation: 492

A second variant will be better, because Instance property check current instance for null value every time.

Upvotes: 0

Related Questions