0

I have seen a singletone code that uses a function without parentheses. i wanted to know is there any difference between a function without parentheses and a function with parentheses that takes no arguments. Here in the code public static MySingleton Instance has no parentheses:
(The code is for unity by the way and written in c#)

public class MySingleton
{
    private static MySingleton instance;

    public MySingleton ()
    {
        if (instance != null)
        {
            Debug.LogError ("Cannot have two instances of singleton.");
            return;
        }
        instance = this;
    }

    public static MySingleton Instance
    {
        get
        {
            if (instance == null)
            {
                new MySingleton ();
            }
        return instance;
        }
    }
}
MickyD
  • 14,343
  • 6
  • 43
  • 67
Parham
  • 21
  • 2
  • 6
  • Read about [Properties](https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx) – Reza Aghaei Oct 02 '15 at 00:05
  • Question linked as [duplicate](http://stackoverflow.com/questions/5096926/what-is-the-get-set-syntax-in-c) explains what "methods without parenthesis" is, and http://stackoverflow.com/questions/601621/properties-vs-methods discusses where to use property or method. Possibly you are asking about something deeper that "what properties are" - may need to clarify what other information you are looking for. – Alexei Levenkov Oct 02 '15 at 00:15
  • Also, just so you know, this implementation of a singleton will never work. You should refer to [this](http://csharpindepth.com/Articles/General/Singleton.aspx). – Joel Bourbonnais Oct 02 '15 at 02:39

2 Answers2

3

While they both seem to operate the same, they are in fact two separate things.

MySingleton() is a Method. (Humility check for not paying attention)

Method are blocks of code that may or may not have parameters that, when invoked, will execute any containing code and either return an object or void.

MySingleton Instance is a Property. Properties allow get/set access to objects within a class/structure.

While theoretically you can do similar things with them, depending on how you are using them; the purpose is quite different.

On a side note, your constructor MySingletion() should be private in a typical Singleton pattern. You can see a few different implementations, by Mr. Skeet, of a singleton here.

2

public static MySingleton Instance is not a method but Property

MSDN Documentation

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

With property you can give access to your private member to outside code. If you give only 'get` access, you make it readonly.

Here you are exposing the instance of your private member as property.

Sateesh Pagolu
  • 8,769
  • 2
  • 24
  • 45
  • would you explain property? – Parham Oct 02 '15 at 00:07
  • 2
    @Parham That's arguably not what this site is about. [Check this out](http://www.amazon.com/Programming-Beginners-Introduction-Step-Step/dp/1507707614/ref=pd_sim_sbs_14_1?ie=UTF8&refRID=0DV56PR06CBXQSV9D6JV&dpID=51j800ipMpL&dpSrc=sims&preST=_AC_UL160_SR107%2C160_) – MickyD Oct 02 '15 at 00:12