Click here to Skip to main content
Click here to Skip to main content

Implement Phonetic ("Sounds-like") Name Searches with Double Metaphone Part V: .NET Implementation

, 19 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Presents a C# implementation of Double Metaphone, for use with any of the .NET languages.


Simple information searches -- name lookups, word searches, etc. -- are often implemented in terms of an exact match criterion. However, given both the diversity of homophonic (pronounced the same) words and names, as well as the propensity for humans to misspell surnames, this simplistic criterion often yields less than desirable results, in the form of reduced result sets, missing records that differ by a misplaced letter or different national spelling.

This article series discusses Lawrence Phillips' Double Metaphone phonetic matching algorithm, and provides several useful implementations, which can be employed in a variety of solutions to create more useful, effective searches of proper names in databases and other collections.


This article series discusses the practical use of the Double Metaphone algorithm to phonetically search name data, using the author's implementations written for C++, COM (Visual Basic, etc.), scripting clients (VBScript, JScript, ASP), SQL, and .NET (C#, VB.NET, and any other .NET language). For a discussion of the Double Metaphone algorithm itself, and Phillips' original code, see Phillips' article in the June 2000 CUJ, available here.

Part I introduces Double Metaphone and describes the author's C++ implementation and its use. Part II discusses the use of the author's COM implementation from within Visual Basic. Part III demonstrates use of the COM implementation from ASP and with VBScript. Part IV shows how to perform phonetic matching within SQL Server using the author's extended stored procedure. Part V demonstrates the author's .NET implementation. Finally, Part VI closes with a survey of phonetic matching alternatives, and pointers to other resources.


Part I of this article series discussed the Double Metaphone algorithm, its origin and use, and the author's C++ implementation. While this section summarizes the key information from that article, readers are encouraged to review the entire article, even if the reader has no C++ experience.

The Double Metaphone algorithm, developed by Lawrence Phillips and published in the June 2000 issue of C/C++ Users Journal, is part of a class of algorithms known as "phonetic matching" or "phonetic encoding" algorithms. These algorithms attempt to detect phonetic ("sounds-like") relationships between words. For example, a phonetic matching algorithm should detect a strong phonetic relationship between "Nelson" and "Nilsen", and no phonetic relationship between "Adam" and "Nelson."

Double Metaphone works by producing one or possibly two phonetic keys, given a word. These keys represent the "sound" of the word. A typical Double Metaphone key is four characters long, as this tends to produce the ideal balance between specificity and generality of results.

The first, or primary, Double Metaphone key represents the American pronunciation of the source word. All words have a primary Double Metaphone key.

The second, or alternate, Double Metaphone key represents an alternate, national pronunciation. For example, many Polish surnames are "Americanized", yielding two possible pronunciations, the original Polish, and the American. For this reason, Double Metaphone computes alternate keys for some words. Note that the vast majority (very roughly, 90%) of words will not yield an alternate key, but when an alternate is computed, it can be pivotal in matching the word.

To compare two words for phonetic similarity, one computes their respective Double Metaphone keys, and then compares each combination:

  • Word 1 Primary - Word 2 Primary
  • Word 1 Primary - Word 2 Alternate
  • Word 1 Alternate - Word 2 Primary
  • Word 1 Alternate - Word 2 Alternate

Obviously if the keys in any of these comparisons are not produced for the given words, the comparisons involving those keys are not performed.

Depending upon which of the above comparisons matches, a match strength is computed. If the first comparison matches, the two words have a strong phonetic similarity. If the second or third comparison matches, the two words have a medium phonetic similarity. If the fourth comparison matches, the two words have a minimal phonetic similarity. Depending upon the particular application requirements, one or more match levels may be excluded from match results.

.NET implementation

The .NET implementation of Double Metaphone is very similar in design and use to the C++ implementation presented in Part I. To use the .NET implementation, simply add the Metaphone.NET.dll assembly to your project's references in Visual Studio. NET, import the nullpointer.Metaphone namespace into the source files, and instantiate the DoubleMetaphone or ShortDoubleMetaphone classes, for string and unsigned short Metaphone keys, respectively.

For example, to compute the Metaphone keys for the name "Nelson", code similar to that listed below may be used (C# code listed; the .NET implementation is callable from VB.NET, J#, and all other .NET languages):

using nullpointer.Metaphone;

DoubleMetaphone mphone = new DoubleMetaphone("Nelson");
System.Console.WriteLine(String.Format("{0} {1}",

Note that the Metaphone keys are obtained via the PrimaryKey and AlternateKey properties.

As with the C++ implementation, an existing instance of a DoubleMetaphone or ShortDoubleMetaphone class can be used to compute the Metaphone keys for a new word, by calling the computeKeys method:

using nullpointer.Metaphone;

DoubleMetaphone mphone = new DoubleMetaphone();
System.Console.WriteLine(String.Format("{0} {1}",

As with all of the implementations presented in this article series, a sample application—CS Word Lookup--written in C# is presented to demonstrate the use of the .NET implementation. CS Word Lookup uses a Hashtable collection class to map Metaphone phonetic keys to an ArrayList class, containing the words which produce the said Metaphone keys.

Performance notes

While the .NET CLR performs reasonably well, it must be stated that the C++ implementation of Double Metaphone will likely perform significantly faster than the .NET version, due primarily to the fact that the C++ version judiciously avoids memory allocation and buffer copies, while the .NET implementation is unable to avoid such constructs. The ambitious reader is encouraged to optimize the .NET implementation, perhaps through the use of the unsafe keyword, to perform direct memory access, at the expense of CLR compliance.


This brief article introduced the author's .NET implementation of Double Metaphone, including code snippets and a brief discussion of performance issues. Continue to Part VI for a review of alternative phonetic matching techniques, and a list of phonetic matching resources, including links to other Double Metaphone implementations.


  • 7-22-03 Initial publication
  • 7-31-03 Added hyperlinks between articles in the series

Article Series


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Adam Nelson
Web Developer
United States United States
My name is Adam Nelson. I've been a professional programmer since 1996, working on everything from database development, early first-generation web applications, modern n-tier distributed apps, high-performance wireless security tools, to my last job as a Senior Consultant at BearingPoint posted in Baghdad, Iraq training Iraqi developers in the wonders of C# and ASP.NET. I am currently an Engineering Director at Dell.
I have a wide range of skills and interests, including cryptography, image processing, computational linguistics, military history, 3D graphics, database optimization, and mathematics, to name a few.

Comments and Discussions

Questionwhat i do for this keyword when i enter it not give output what i want? Pinmemberashishgupta121222-Oct-14 2:35 
SuggestionModified code for more idiomatic, C# portable library PinmemberDaniele Fusi24-May-14 0:46 
QuestionXPMetaphone in 64 bit? PinmemberDunc_NZ10-Apr-11 11:32 
GeneralStrongly signed dll PinmemberChris Copac2-Mar-11 11:59 
GeneralRe: Strongly signed dll PinmemberAdam Nelson15-Mar-11 15:15 
GeneralNice implementation of a killer algorithm PinmemberDimitri Troncquo22-Feb-11 23:55 
GeneralRe: Nice implementation of a killer algorithm PinmemberAdam Nelson23-Feb-11 5:02 
Generalhelp implementing this in Pinmembervelascojames23-Aug-10 23:59 
Generaldll required PinmemberPrathapavidyadaran8-Sep-09 9:43 
GeneralSuper implementation thanks PinmemberPaul Sinnema12-Mar-09 7:25 
GeneralRe: Super implementation thanks PinmemberAdam Nelson12-Mar-09 11:28 
QuestionLicensing? PinmemberCasey Gum10-Nov-08 11:40 
AnswerRe: Licensing? PinmemberAdam Nelson10-Nov-08 12:11 
QuestionMetaphone.NET.dll Pinmembersanjutvj30-May-07 1:52 
QuestionError on x64 bit compile Pinmemberterry091716-Apr-07 6:30 
AnswerRe: Error on x64 bit compile Pinmembercp197017-Apr-07 5:31 
Question.NET 2.0 & Inconsistent results PinmemberMike Renno5-Dec-06 4:49 
AnswerRe: .NET 2.0 & Inconsistent results PinmemberAdam Nelson5-Dec-06 6:49 
GeneralFound a couple of bugs PinmemberMike Renno9-Jan-07 11:16 
GeneralRe: Found a couple of bugs PinmemberAdam Nelson1-Feb-07 6:29 
GeneralRe: Found a couple of bugs PinmemberMike Renno6-Feb-07 4:47 
Thanks for your response. We ran into a few glitches with the CLR implementation for SQL Server:
1. SQL Server apparently doesn't allow namespaces in CLR classes, so we had to remove this from your original source.
2. Only a single dll file can be registered via the CREATE ASSEMBLY statement, so we had to combine the source files in order to use ShortDoubleMetaphone.
3. A SQL scalar UDF can only return a single parameter, so there wasn't a clean mapping to replace xp_metaphone with its separate output parameters for the primary and alternate metaphone keys. We opted to combine the values into a single BINARY(4) output parameter and then parse this back into two SMALLINTs after the UDF call, but this seems like a kludge. This is also where we ran into the glitch for the "WF" input parameter - we got x0000 back instead of the expected xFFFF for the alternate key.
What we have is working, but I would still be interested in your thoughts regarding a well-thought-out approach for SQL 2005.
Sorry if my 16:16 9 Jan '07 posting was unclear - the exception occurred at line 139 for an input of "C" rather than "CAESAR". The change to use 5 spaces of padding has corrected this.
Thanks again for your good work on this.
Mike Renno

GeneralRe: Found a couple of bugs PinmemberAdam Nelson1-Feb-07 6:43 
QuestionChecking for null alternate keys with unsigned short Pinmembermill40237-Aug-06 12:25 
AnswerRe: Checking for null alternate keys with unsigned short PinmemberAdam Nelson25-Sep-06 5:11 
GeneralRe: Checking for null alternate keys with unsigned short Pinmembermill402325-Sep-06 6:37 
GeneralRe: Checking for null alternate keys with unsigned short PinmemberAdam Nelson25-Sep-06 6:52 
QuestionPossible bug? Pinmemberwiseleyb4-Dec-05 6:44 
AnswerRe: Possible bug? PinmemberAdam Nelson4-Dec-05 11:56 
GeneralEditex algorithm PinmemberElvio Fernandez11-Dec-05 9:43 
GeneralNice Work! Pinmembertequilacollins23-May-05 8:39 
GeneralRe: Nice Work! PinmemberAdam Nelson23-May-05 10:45 
GeneralRe: Nice Work! PinsussAnonymous23-May-05 15:26 
QuestionDoes the classic version and your produce the same results? Pinmembergaryfis9-Jun-04 11:51 
AnswerRe: Does the classic version and your produce the same results? PinmemberAdam Nelson9-Jun-04 13:25 
GeneralGerman Language Pinmemberkuerbis31-Jul-03 7:57 
GeneralRe: German Language PinmemberAdam Nelson31-Jul-03 8:23 
GeneralRe: German Language PinmemberPatrick Blackman6-Oct-03 17:34 
GeneralRe: German Language PinmemberAdam Nelson6-Oct-03 18:16 
GeneralRe: German Language PinmemberPatrick Blackman6-Oct-03 19:57 
GeneralRe: German Language PinmemberAdam Nelson7-Oct-03 3:26 
GeneralPrevious articles Pinmemberydlm31-Jul-03 3:36 
GeneralRe: Previous articles PinmemberAdam Nelson31-Jul-03 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 19 Mar 2007
Article Copyright 2003 by Adam Nelson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid