-1

Im trying to test my singleton object in C# but somehow not satisfied on how I brute force instantiating the object(using parallel foreach).

Is there a right way/better way to test it?

NumbahOneCoder
  • 119
  • 1
  • 1
  • 5
  • 1
    If you have more then 1 instance of singleton - its not a singleton. Why do you need to test this? – vasily.sib Jan 22 '20 at 04:44
  • @vasily.sib to make sure that my singleton is working properly – NumbahOneCoder Jan 22 '20 at 04:51
  • 1
    If you *post the code*, then someone can do very basic static analysis of that code to tell you if it's even possible to instantiate the class more than once. Then if it is, the discussion can move on to how to improve the design so it's not possible. – madreflection Jan 22 '20 at 04:58
  • 2
    Hi. I am marking this as a duplicate but I want you to know why. [Thread Safe C# Singleton Pattern](https://stackoverflow.com/questions/12316406/thread-safe-c-sharp-singleton-pattern) There are a number of tried and true C# methods to create a singleton. This linked article provides options for that as well as links to Jon Skeet's great breakdown of thread safe singletons. While your question is asking specifically about testing memory allocation, but is not formatted as a proper SO question. "Better way" questions are considered opinion based and not a proper SO question. – jwatts1980 Jan 22 '20 at 05:08
  • 1
    Additionally, if you want to know if the singleton is being instantiated more than once, place a breakpoint at that point of the code and debug your program. Then you will be able to see how many times it was hit. – jwatts1980 Jan 22 '20 at 05:12

2 Answers2

0

I don't understand what you mean with "parallel foreach". A singleton is implemented like this:

public class MyClass
{
    private static MyClass _instance;

    private MyClass()
    {
        //Do Stuff
    }

    public static MyClass GetInstance()
    {
         if(_instance == null)
             _instance = new MyClass();

         return _instance;
    }
}

Another way, instead of a Method is a Property:

private static readonly object LockObject = new object();
private static MyClass _instance;

public static MyClass Instance
{
    get
    {
        lock (LockObject)
        {
            return _instance ?? (_instance = new MyClass());
        }
    }
}

While I prefer the first Method, cause it's much easier to implement, even for beginners, the Property is also a good way

DudeWhoWantsToLearn
  • 751
  • 2
  • 10
  • 29
  • @DudeWhoWantsToLearn, check out Microsoft's docs on parallel foreach: https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop It "schedules the work on multiple threads" to complete the loop faster. Thus, a singleton that is used in inside a parallel foreach needs to be be thread safe. Your first example is not thread safe. Your second example is, but locks are somewhat expensive and you'll be hitting it one or more times for every item in the loop. Please check out Jon Skeet's article: https://csharpindepth.com/Articles/Singleton – jwatts1980 Jan 23 '20 at 05:20
-1

Singleton class can have only one instance, If it is initialized multiple times then that means you have not correctly implemented the Singleton Design.

You can check the instance.Hashcode() value, it must remain same wherever you are using the instance of that Singleton class.

Bhupesh
  • 35
  • 7
  • Hi Bhupesh i appreciate your answwer but is there a better way to test my singleton object if its working properly? what i currently did is to execute getInstance by using multithreading but I'm not satisfied with what ive done – NumbahOneCoder Jan 22 '20 at 04:52
  • You can create a List and can store the instances of the Singleton class in that - If you have correctly implemented all the instances must be same. Now from that list iterate through each object and check if all the instances have same Hashcode value or not. If values are same then you have implemented the Singleton design. – Bhupesh Jan 22 '20 at 04:57
  • 1
    if `.Hashcode()` of 2 objects are equal, this doesn't mean that this is the same object. Use `Object.ReferenceEquals(objA, objB)` method. – vasily.sib Jan 22 '20 at 05:07
  • You guys are getting a bit spun out on this. Check out the suggested duplicate. Testing for multiple instances becomes a non-issue if it's implemented correctly. – madreflection Jan 22 '20 at 05:09
  • Thanks Vasily, I didn't know that. I always thought that hashcodes are unique. – Bhupesh Jan 22 '20 at 05:10
  • 1
    @Bhupesh: In that case, make sure you read this: https://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/ – madreflection Jan 22 '20 at 05:11