Let us learn PluralizationServices of .NET 4.0

Posted by in C# category on for Beginner level | Points: 250 | Views : 8639 red flag

In this article, we will explore the PluralizationServices of .NET 4.0

 Download source code for Let us learn PluralizationServices of .NET 4.0


Sometimes we may need to get the plural or singular of words.We can build our own by creating our data structure.However, from dotnet 4.0, we have the "PluralizationServices" that helps us to do so.We can find it in System.Data.Entity.Design.PluralizationServices.PluralizationService

In this article, we will look into it.

So what is "PluralizationService"?

The "PluralizationService" is an abstract class and provides methods for constructing plural and singular forms of words.

It is found in the System.Data.Entity.Design.dll under the System.Data.Entity.Design.PluralizationServices namespace. It has the below methods and properties

As can be figure out that,it is an abstract class and so cannot be directly instantiated.However, there is a static method CreateService that helps to do so(we will see that in code example).This method creates a System.Data.Entity.Design.PluralizationServices.PluralizationService that applies language rules that correspond to the specified System.Globalization.CultureInfo.At present, it supports only "en-us".

The IsPlural method returns a boolean "true" if the specified word is plural.

The IsSingular method returns a boolean "true" if the specified word is singular.

The Pluralize method returns the plural form of the specified word.

The Singularize method returns the singular form of the specified word.

Straight to Experiment

Fire up a console application and add reference to "System.Data.Entity.Design".Make sure that the Target Framework is .NET Framework 4 (is not .NET Framework Client Profile)

Next write the below code

 class Program
	static void Main(string[] args)
		//creates an instance of the service
		PluralizationService ps = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));

		//check if the supplied word is plural
		var isPlural = ps.IsPlural("mangoes"); //true

		//check if the supplied word is singular
		var isSingular = ps.IsSingular("mangoe"); //true

		//change a singular word to plural
		var pluralWord = ps.Pluralize("boy"); //result: boys

		//change a plural word to singular
		var singularWord = ps.Singularize("girls"); //result: girl


The code is self explanatory.We are just playing with the various methods being exposed with the API.

A word about ICustomPluralizationMapping

We can however, create our own singular and plural words.For that reason, we need the help of "ICustomPluralizationMapping" interface.It represents a collection of the singular and plural forms of words.

It has only one method

void AddWord(string singular, string plural)

The method adds singular and plural forms of a word to the System.Data.Entity.Design.PluralizationServices.ICustomPluralizationMapping object.So let us put it into experiment

//Add custom singular and plural word using the AddWord method of ICustomPluralizationMapping 
ICustomPluralizationMapping customMapping = ps as ICustomPluralizationMapping;

// Add a new word mapping 
customMapping.AddWord("daughter-in-law", "daughters-in-law");

var thePlural = ps.Pluralize("daughter-in-law"); //result: daughters-in-law
var theSingular = ps.Singularize("daughters-in-law"); //result: daughter-in-law


PluralizationService Class


Hope this tutorial has given a little insight about the PluralizationServices API of dotnet4.0.This may be helpful to others.Thanks for reading.Zipped file is attached herewith.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)