
You want to build a layer of abstraction over the pluralization of words and for obtaining a plural word from a singular noun. In many cases, using a lookup table of known values can provide better performance, smaller memory footprint, and fewer errors than more complicated solutions.
Initially here, we look at a program text that provides a simple abstraction on the procedure of obtaining a plural word from a singular word based on the count. This means it can be used to get the phrase "2 results" from the values (2) and "result".
The implementation is not advanced but offers the benefit of simplicity and can improve the overall structure of certain programs. Because the problem of parsing English text is extremely difficult, this class simply uses hard-coded values and does a lookup.
This C# program implements word pluralization using a lookup table.
Program that resolves plural words [C#]
using System;
using System.Collections.Generic;
public static class Plurals
{
static Dictionary<string, string> _pluralTable =
new Dictionary<string, string>
{
{"entry", "entries"},
{"image", "images"},
{"view", "views"},
{"file", "files"},
{"result", "results"},
{"word", "words"},
{"definition", "definitions"},
{"item", "items"},
{"megabyte", "megabytes"},
{"game", "games"}
};
public static string Plural(string word, int count)
{
// Special-case count of one.
// ... Otherwise, return the pluralized word.
if (count == 1)
{
return word;
}
return _pluralTable[word];
}
public static string PluralPhrase(string word, int count)
{
// Returns phrase of complete pluralized phrase.
// .. Such as "3 files".
string properPlural = Plural(word, count);
return count.ToString() + " " + properPlural;
}
}
class Program
{
static void Main()
{
Console.WriteLine(Plurals.Plural("word", 0));
Console.WriteLine(Plurals.Plural("word", 1));
Console.WriteLine(Plurals.Plural("word", 2));
Console.WriteLine(Plurals.PluralPhrase("view", 0));
Console.WriteLine(Plurals.PluralPhrase("view", 1));
Console.WriteLine(Plurals.PluralPhrase("view", 2));
}
}
Output
words
word
words
0 views
1 view
2 views
Methods. This program text is a compilation unit and it declares two classes, the Plurals class and the Program class. The Plurals class introduces two methods to the program, both of which receive two parameters.
The Plural method receives the word and the count of the word in the sentence; it returns a version of the word that it is correctly pluralized if located in the Dictionary. The PluralPhrase method receives the same parameters, but returns a string with the original number in the resulting string as well.
Static Dictionary storage. This source text demonstrates the static Dictionary as well. You can use a static Dictionary and use a field initializer to populate it with values before it is used. In this program, the indexer on the Dictionary is used, which will throw an exception if the string is not found. The methods provide fast lookup even if you have hundreds of strings in the Dictionary. You can populate the Dictionary through a text file if you need to.

Declarative, expression-based programming styles can benefit certain program requirements. In some simple programs, a ternary expression or if-statement is used to handle the English plural rules for a count of one. However, if this pattern is repeated often, it can become a burden on the overall structure. In this case, using a static and immutable data structure can simplify programs. This provides another level of indirection, which also can provide other benefits.

Here we looked at a simple class that demonstrates a private and static Dictionary when used as a data structure for providing plural words in the C# language. The program receives singular words and looks these words up to form singular words.
Algorithms