In order to find duplicate rows from your table (eg. Customer) you can use this snippet:

Transact-SQL

1

2

3

4

5

SELECTFirstName,COUNT(*)TotalCount

FROMCustomer

GROUPBYFirstName

HAVINGCOUNT(*)>1

ORDERBYCOUNT(*)DESC

Following code is useful to delete duplicate records. The table must have identity column, which will be used to identify the duplicate records. For table in example I added CustomerID as Identity Column and Columns which have duplicate data are FirstName and LastName.

A Singleton class is one where there is only ever one instance of the class created. This means that constructors must be private to avoid users creating their own instances, and a static property (or method in languages without properties) is defined that returns a single static instance.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

publicclassSingleton

{

// the single instance is defined in a static field

privatestaticreadonlySingleton _instance=newSingleton();

// constructor private so users can't instantiate on their own

privateSingleton()

{

}

// read-only property that returns the static field

publicstaticSingletonInstance

{

get

{

return_instance;

}

}

}

This is the most basic singleton, notice the key features:

Static readonly field that contains the one and only instance.

Constructor is private so it can only be called by the class itself.

Static property that returns the single instance.

Looks like it satisfies, right? There’s just one (potential) problem. C# gives you no guarantee of when the static field _instance will be created. This is because the C# standard simply states that classes (which are marked in the IL as BeforeFieldInit) can have their static fields initialized any time before the field is accessed. This means that they may be initialized on first use, they may be initialized at some other time before, you can’t be sure when.

So what if you want to guarantee your instance is truly lazy. That is, that it is only created on first call to Instance?

First option is to use lazy construction ourselves, but being that our Singleton may be accessed by many different threads, we’d need to lock it down.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

publicclassLazySingleton1

{

// lock for thread-safety laziness

privatestaticreadonlyobject_mutex=newobject();

// static field to hold single instance

privatestaticvolatileLazySingleton1 _instance=null;

// property that does some locking and then creates on first call

publicstaticLazySingleton1Instance

{

get

{

if(_instance==null)

{

lock(_mutex)

{

if(_instance==null)

{

_instance=newLazySingleton1();

}

}

}

return_instance;

}

}

privateLazySingleton1()

{

}

}

This is a standard double-check algorithm so that you don’t lock if the instance has already been created. However, because it’s possible two threads can go through the first if at the same time the first time back in, you need to check again after the lock is acquired to avoid creating two instances.

Second option is to take advantage of the C# standard’s BeforeFieldInit and define your class with a static constructor. It need not have a body, just the presence of the static constructor will remove the BeforeFieldInit attribute on the class and guarantee that no fields are initialized until the first static field, property, or method is called.

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

publicclassLazySingleton2

{

// because of the static constructor, this won't get created until first use

privatestaticreadonlyLazySingleton2 _instance=newLazySingleton2();

// Returns the singleton instance using lazy-instantiation

publicstaticLazySingleton2Instance

{

get{return_instance;}

}

// private to prevent direct instantiation

privateLazySingleton2()

{

}

// removes BeforeFieldInit on class so static fields not

// initialized before they are used

staticLazySingleton2()

{

}

}

Third option is to use .Net 4.0 new features, System.Lazy type which guarantees thread-safe lazy-construction. Using System.Lazy, we get:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

publicclassLazySingleton3

{

// static holder for instance, need to use lambda to construct since constructor private

privatestaticreadonlyLazy<LazySingleton3>_instance

=newLazy<LazySingleton3>(()=>newLazySingleton3());

// private to prevent direct instantiation.

privateLazySingleton3()

{

}

// accessor for instance

publicstaticLazySingleton3Instance

{

get

{

return_instance.Value;

}

}

}

Note, you need your lambda to call the private constructor as Lazy’s default constructor can only call public constructors of the type passed in (which we can’t have by definition of a Singleton). But, because the lambda is defined inside our type, it has access to the private members so it’s perfect.

Lazy has many other uses as well, obviously, but I really love how elegant and readable it makes the lazy Singleton.