Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put conversion functions?

As C# lacks support for freestanding functions, I find it hard to find a place to put conversion functions. For example, I'd like to convert an enum to a number. In C++, I would make the following freestanding function for this:

UINT32 ConvertToUint32(const MyEnum e);

How can I elegantly do this in C#? Should I make a dummy static class for holding the function, and if so, how can I find a meaningful name for it? Or should I make a partial class Convert?

Any ideas?

Thanks in advance.

Update: In retrospect, my example was not very well chosen, as there exists a default conversion between enum and int. This would be a better example:

Person ConvertToPerson(const SpecialPersonsEnum e);
like image 789
Dimitri C. Avatar asked Mar 04 '10 07:03

Dimitri C.


1 Answers

The above example looks like a candidate for an Extension Method.
If that's not possible, I define them as static methods in a static class ; I'd normally put them in a static class called XXXHelper

like image 126
Gishu Avatar answered Oct 22 '22 20:10

Gishu