---
title: "Doubleton Design Pattern in C# – Part II"  
description: "I would like to share a way by which we can create a class which could create maximum two objects."  
author: "Devesh Omar"  
published: 2014-06-04  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1409/doubleton-design-pattern-in-c-sharp-part-ii  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Doubleton Design Pattern in C# – Part II

I would like to share a way by which we can create a class which could create maximum two objects.\

This could be extension of Singleton pattern.

I would like to introduce following way to create doubleton class.

This Article could be extension of my last article.

##### Code Sample

```
sealed class doubleton2    {        private static readonly object doubletonLock = new object();        private static int index = 0;        private static List<doubleton2> list = new List<doubleton2>();          private doubleton2() { }         public static List<doubleton2> ReturnDoubletonList()         {                     lock (doubletonLock)             {                 if (index < 2)                 {                     list.Add(new doubleton2());                     index++;                 }              }             return list;          }       }       class Program    {        static void Main(string[] args)        {           List<doubleton2> lifirst = doubleton2.ReturnDoubletonList();           List<doubleton2> lisecond= doubleton2.ReturnDoubletonList();           List<doubleton2> lithird = doubleton2.ReturnDoubletonList();           Console.ReadKey();         }    } 
```

##### Understanding the code

a. We created List object of doubleton2 class

b. On each call of ReturnDoubletonList() method we are just incrementing the

value of this static field (index integer)

c. ReturnDoubletonList () method is static and it returns List object of

Doubleton class

d. We have index<2 inside ReturnDoubletonList () method to make sure we can

have maximum two objects of doubleton class.

##### \
After executing first line on main function

```
List<doubleton2> lifirst = doubleton2.ReturnDoubletonList();We have a list of one object.
```

![Doubleton Design Pattern in C# – Part II](http://www.mindstick.com/MindStickArticle/8aa6fb13-75ff-44d1-b22f-abba07d01d2b/Images/image001.png)

6. After executing second line on main function

\

```
List<doubleton2> liSecond = doubleton2.ReturnDoubletonList();We have a list of two objects.
```

![Doubleton Design Pattern in C# – Part II](http://www.mindstick.com/MindStickArticle/8aa6fb13-75ff-44d1-b22f-abba07d01d2b/Images/image002.png)

7. After executing third line on main function

\

```
List<doubleton2> lithird = doubleton2.ReturnDoubletonList();Again we have a list of two objects because we have index<2 check in ReturndoubetonList function.
```

\

![Doubleton Design Pattern in C# – Part II](http://www.mindstick.com/MindStickArticle/8aa6fb13-75ff-44d1-b22f-abba07d01d2b/Images/image003.png)

##### \

##### Facts with the code

\

Here in above code following things could be note down.

1. Object lifirst[0] is always equal to lisecond[0].
2. If I added following line to main function

Console.WriteLine("lifirst[0] and lisecond[0] are Equal: " +

lifirst[0].Equals(lisecond[0]));

##### Then following would be output.

![Doubleton Design Pattern in C# – Part II](http://www.mindstick.com/MindStickArticle/8aa6fb13-75ff-44d1-b22f-abba07d01d2b/Images/image004.png)

##### \
Conclusion

From above way we can learn how to create doubleton class. Similarly we can N-

ton class as well.

We can modify this class as per requirement.

---

Original Source: https://www.mindstick.com/articles/1409/doubleton-design-pattern-in-c-sharp-part-ii

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
