Trying to implement singleton gives me stackoverflow error c#

I am trying to make a singleton to account all the controllers I am going to create. When I run the website I get the stackoverflow error. This is my singleton class:

public class MainController 
{
    private DatabaseController databaseController;
    private UserDBController userDBController;
    private static MainController MCinstance = null;
    public MainController()
    {
        if (MCinstance == null)
        {
            databaseController = new DatabaseController();
            userDBController = new UserDBController();
        }
    }
    public static MainController Instance
    {
        get
        {
            if (MCinstance == null)
            {
                MCinstance = new MainController();
            }
            return MCinstance;
        }
    }
    public UserDBController GetUserDBController()
    {
        return userDBController;
    }
    public DatabaseController GetDBController()
    {
        return databaseController;
    }
}

This is how I use the singleton class in a different class:

private UserDBController userDBController = MainController.Instance.GetUserDBController();

What am I doing wrong?

Answers


You can change your implemention like below,

public class MainController
{
    private DatabaseController databaseController;
    private UserDBController userDBController;
    private static MainController MCinstance = null;

    static MainController()
    {
        MCinstance = new MainController();
        MCinstance.databaseController = new DatabaseController();
        MCinstance.userDBController = new UserDBController();
    }

    public static MainController Instance
    {
        get
        {               
            return MCinstance;
        }
    }

    public UserDBController GetUserDBController()
    {
        return userDBController;
    }

    public DatabaseController GetDBController()
    {
        return databaseController;
    }
}

You should add the readonly modifier to your MCinstance declaration and instantiate it only once in static constructor.

public class MainController 
{

    public static readonly MainController Instance = null;
    static MainController()
    {
        Instance  = new MainController()
    }
}

Need Your Help

MPMoviePlayerController goes blank when scrolling in UICollectionViewCell

ios objective-c uicollectionview avfoundation mpmovieplayercontroller

I have a horizontal flow layout for my UICollectionView. Each cell takes up the entire screen and paging is on. Each cell also contains an MPMoviePlayerController and I'm able to playback video w...

why do we need an extra layout when using AutoCompleteTextView

android xml autocompletetextview

Check the code at http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.