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;
        }
    }
}
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

2

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.

  • _"MySingleton() is a method."_ - Incorrect. `MySingleton()` is a _constructor_ not a _"method"_. One does not _invoke_ a constructor in the same way as a method. Constructors have no return value whilst methods can. –  Oct 02 '15 at 00:15
  • You're absolutely correct, I was a bit too focused on the title and not what was actually presented. I'll update accordingly when I get a chance. – Richard Preziosi Oct 02 '15 at 00:56
  • No problem Richard, let me know when you edit so I can remove the -1 :) Have a good day –  Oct 02 '15 at 02:01
  • @Roy, I've updated the answer. You have a great day as well. – Richard Preziosi Oct 02 '15 at 11:55
  • Thank-you good sir. +1 –  Oct 02 '15 at 12:51
1

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
  • 9,282
  • 2
  • 30
  • 48
  • 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_) –  Oct 02 '15 at 00:12