How to use this Singleton class in C #?

I am struggling using the Singleton design pattern. I am trying to use it in this simple console application. I have a problem with it in the Main method in the Program class. I want to define an object from the Singleton class, for example: var data = Singleton.Instance; but I don’t know why I can’t do this. Also, I don’t know why I get the following error message when starting the program:

 Unhandled Exception: System.NullRefernceException: Object reference not set to an instance of an object. 

So how to fix this?
Singleton Class:

 namespace Singleton { class Singleton { //Variable private static Singleton instance; private List<string> Messages; //Constructor private Singleton() { } //Property public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } } //Methods public void Message(string message) { Messages.Add(message); } public bool HasMessage(string message) { return Messages.Contains(message); } } } 

Program Class:

 namespace Singleton { class Program { static void Main(string[] args) { var data = Singleton.Instance; Singleton.Instance.Message("Hello World!!!"); if(Singleton.Instance.HasMessage("12")) Console.WriteLine("NO STRING!!!"); else Console.WriteLine("There is a match"); } } } 

UPDATE:

Guys, I really appreciate your help so far. Now the program works, but the logic does not work. If you look at the main program, you will see that there is only "Hello World !!!" in the list. However, when I used the HasMessage method, this did not work. Because the program continues to show "There is a match." But he must show me "NO LINE !!!" since there is no coincidence. So how to fix this?
+8
c # design-patterns
source share
5 answers

Your Messages field is not initialized by anything. That is why you get an exception. In your class, do:

  private List<string> Messages = new List<string>(); 

You can also watch Thread Safe Singleton implementation of Jon Skeet

EDIT:

Based on the updated question. Your check and message are the opposite. It should be:

 if (Singleton.Instance.HasMessage("12")) Console.WriteLine("There is a match"); else Console.WriteLine("NO STRING!!!"); 

Your HasMessage method returns true if the passed parameter is present in the list, and false otherwise.

+12
source share

Looks like you're almost there. Consider rewriting your code as follows:

 class Singleton { //Variable private static Singleton Instance; private List<string> Messages; //Constructor private Singleton() { Messages = new List<string>(); //Make sure to instantiate instance types before use. } //Property public static Singleton GetInstance() { if (Instance == null) { Instance = new Singleton(); } return Instance; } //Methods public void Message(string message) { Messages.Add(message); } public bool HasMessage(string message) { return Messages.Contains(message); } } 

This site has some useful C # tutorials for design patterns.

+4
source share

However, when I used the HasMessage method, this did not work. Because the program continues to show "There is a match." But he must show me "NO LINE !!!" since there is no coincidence. So how to fix this?

This should be a separate question, but I will answer anyway. You have your fortune back. Your code says “no line” if the instance contains the message “12”, and “There is a match” if it is not. Try the following:

  if(Singleton.Instance.HasMessage("12")) Console.WriteLine("There is a match"); else Console.WriteLine("NO STRING!!!"); 
+4
source share
 private List<String> Messages; 

Here is your problem. This member has never been used in your code. You can do the following:

  //Constructor private Singleton() { Messages = new List<string>(); } 

I also suggest that you use the correct naming conventions for your local variables and members. Change instance to m_Instance and Messages to m_Messages . Also try to implement singletones in a thread-safe way ... see this page for more information.

+1
source share

What the Singleton implementation is:

 public sealed class SingletonExample { //static Field private static SingletonExample _seInstance = null; private int _nCounter = 0; // private constructor private SingletonExample() { _nCounter = 1; } //public static get(), with creating only one instance EVER public static SingletonExample SeInstance { get { return _seInstance ?? (_seInstance = new SingletonExample()); } } } 

How to call and create an instance?

 SingletonExample si1 = SingletonExample.SeInstance; SingletonExample si2 = SingletonExample.SeInstance; // it will be the same object System.Diagnostics.Debug.WriteLine(si1.Equals(si2));// TRUE 
0
source share

All Articles